Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8672769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:17:10+00:00 2026-06-12T19:17:10+00:00

I have different tables in my MYSQL database and I need to create a

  • 0

I have different tables in my MYSQL database and I need to create a SELECT query that will check certain values before selecting a row.

It’s basically 3 tables (news, tags and news_tags).

  1. news contains information about a news.
  2. tags information about tags.
  3. news_tags store which tag is used by a news.

Here are my 3 different tables (simplified of course)

To illustrate:

news:

id | status
1  | 1
2  | 1
3  | 0

tags:

tag_id | tag_name
1      | name 1 
2      | name 2

news_tags:

id | news_id | tag_id
1  | 1       | 1
2  | 1       | 2
3  | 2       | 1
4  | 3       | 1
5  | 3       | 2

Now, at first I wanted to know how many times each tag was being used.
So I came up with this query

SELECT tags.tag_name, news_tags.tag_id, COUNT(*) as count 
FROM news_tags 
LEFT JOIN tags 
ON tags.tag_id = news_tags.tag_id 
GROUP BY news_tags.tag_id 
ORDER BY count DESC

This give me the following output

tag_name | count
name 1   | 3 
name 2   | 2

Now my problem is that I don’t want to count those that the news.status = 0;
So if we look at my example, in the news table the row with the id = 3 has a status = 0.
The count should not take in effect where news_id = 3 in the news_tags table.

Basically the desired output should be

tag_name | count
name 1   | 2 
name 2   | 1

Thank you for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:17:11+00:00Added an answer on June 12, 2026 at 7:17 pm

    You need to join it with news table so you can query for status

    SELECT tags.tag_name, news_tags.tag_id, COUNT(*) as count 
    FROM    news_tags 
            LEFT JOIN tags 
                ON tags.tag_id = news_tags.tag_id 
            LEFT JOIN news
                ON news.id = news_tags.news_id
    WHERE   news.status = 1
    GROUP BY news_tags.tag_id 
    ORDER BY count DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two different tables, lead and click. I would like to query MySQL
I will have multiple tables used by different projects on the same mySql server.
I have two different tables from which I need to pull data blogs which
I have 4 different tables that I want to join. The tables are structured
I have four different tables, one main SPECIAL table containing only id's that reference
I have a ReferenceID varchar(6) column in over 80 different tables. I need to
I have an existing database with users and administrators in different tables. I am
I'm working on a mysql query in a Drupal database that pulls together users
I have 2 tables(Companies and Contacts). I need create a list of all of
I have a client that wants to connect to his MySQL database using Dial-up/Modem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.