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 353501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:50:10+00:00 2026-05-12T11:50:10+00:00

If anyone could recommend a good book for learning mySQL as well, that would

  • 0

If anyone could recommend a good book for learning mySQL as well, that would be great :).

I have two tables, tags, codes_tags

CREATE TABLE `tags` (
 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
 `name` varchar(40) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
 PRIMARY KEY (`id`),
 UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=190 DEFAULT CHARSET=utf8


CREATE TABLE `codes_tags` (
 `code_id` int(11) unsigned NOT NULL,
 `tag_id` int(11) unsigned NOT NULL,
 KEY `sourcecode_id` (`code_id`),
 KEY `tag_id` (`tag_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

What I am trying to do is select the name from ‘tags’, and how many of that tag_id there are in ‘codes_tags’, and order them by that count. If there is no records in codes_tags for that tag_id, ‘count’ should be equal to 0 or NULL (preferably 0).

This is the closest I have come so far:

SELECT tags.name, COUNT( codes_tags.tag_id ) AS count
FROM tags
LEFT JOIN codes_tags ON tags.id = codes_tags.tag_id
GROUP BY tag_id
ORDER BY count DESC
LIMIT 0 , 30

It seems to do what I am wanting, however it is only returning four rows when it should return 30.

What am I doing wrong here?
Thanks.

  • 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-05-12T11:50:10+00:00Added an answer on May 12, 2026 at 11:50 am

    I think if you change your COUNT(codes_tags.tag_id) to COUNT(*) in the SELECT, that NULLs will also be included. (If it’s nulls or 0 counts that you’re missing. Otherwise, the query looks fine).

    EDIT: On second thought, I missed the LEFT JOIN. That would mean you want all of the tags even if they’re not related to something in the codes_tags table. Is that what you want?

    I would probably do something like the following:

    SELECT tags.name, COUNT(*) AS count
    FROM tags
    INNER JOIN codes_tags ON tags.id = codes_tags.tag_id
    GROUP BY tags.id
    ORDER BY count(*) DESC
    

    It can be inferred from the items not in the list which tags are not also included in codes_tags. However, if you wanted to explicitly do that as well:

    SELECT tags.name, COUNT(*) AS count
    FROM tags
    INNER JOIN codes_tags ON tags.id = codes_tags.tag_id
    GROUP BY tags.id
    UNION
    SELECT tags.name, '0'
    from tags
    where tags.name not in 
       (SELECT tags.name
       FROM tags
       INNER JOIN codes_tags ON tags.id = codes_tags.tag_id)
    ORDER BY count(*) DESC
    

    (I don’t have access to a SQL box at the moment, so take the queries with a grain of salt; they’re untested.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone recommend a good application that could be used to convert VB.NET projects
Could anyone recommend good book about programming in windows 7 in C++?
i want to learn animation in android. Could anyone please recommend a good book
Anyone could recommend a good guide/tutorial/article with tips/guidelines in how to organize and partition
Could anyone recommend a good BAML Decompiler / Viewer besides BAML Viewer plugin for
Can anyone recommend some useful performance analysis tools for PHP scripts? Anything that could
I was wondering if anyone could recommend a good LOC counter for .NET projects
Could anyone recommend any tools to be able to see where external files /
Could anyone could point me to some code/give me ideas on how to create
I was wondering if anyone could point to an Open Source date utility class

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.