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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:21:23+00:00 2026-05-10T21:21:23+00:00

Often you need to show a list of database items and certain aggregate numbers

  • 0

Often you need to show a list of database items and certain aggregate numbers about each item. For instance, when you type the title text on Stack Overflow, the Related Questions list appears. The list shows the titles of related entries and the single aggregated number of quantity of responses for each title.

I have a similar problem but needing multiple aggregates. I’d like to display a list of items in any of 3 formats depending on user options:

  • My item’s name (15 total, 13 owned by me)
  • My item’s name (15 total)
  • My item’s name (13 owned by me)

My database is:

  • items: itemId, itemName, ownerId
  • categories: catId, catName
  • map: mapId, itemId, catId

The query below gets: category name, count of item ids per category

SELECT   categories.catName,   COUNT(map.itemId) AS item_count FROM categories LEFT JOIN map   ON categories.catId = map.catId GROUP BY categories.catName 

This one gets: category name, count of item ids per category for this owner_id only

SELECT categories.catName,  COUNT(map.itemId) AS owner_item_count FROM categories LEFT JOIN map   ON categories.catId = map.catId LEFT JOIN items   ON items.itemId = map.itemId WHERE owner = @ownerId GROUP BY categories.catId 

But how do i get them at the same time in a single query? I.e.: category name, count of item ids per category, count of item ids per category for this owner_id only

Bonus. How can I optionally only retrieve where catId count != 0 for any of these? In trying ‘WHERE item_count <> 0’ I get:

MySQL said: Documentation #1054 - Unknown column 'rid_count' in 'where clause'  
  • 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. 2026-05-10T21:21:24+00:00Added an answer on May 10, 2026 at 9:21 pm

    Here’s a trick: calculating a SUM() of values that are known to be either 1 or 0 is equivalent to a COUNT() of the rows where the value is 1. And you know that a boolean comparison returns 1 or 0 (or NULL).

    SELECT c.catname, COUNT(m.catid) AS item_count,   SUM(i.ownerid = @ownerid) AS owner_item_count FROM categories c   LEFT JOIN map m USING (catid)   LEFT JOIN items i USING (itemid) GROUP BY c.catid; 

    As for the bonus question, you could simply do an inner join instead of an outer join, which would mean only categories with at least one row in map would be returned.

    SELECT c.catname, COUNT(m.catid) AS item_count,   SUM(i.ownerid = @ownerid) AS owner_item_count FROM categories c   INNER JOIN map m USING (catid)   INNER JOIN items i USING (itemid) GROUP BY c.catid; 

    Here’s another solution, which is not as efficient but I’ll show it to explain why you got the error:

    SELECT c.catname, COUNT(m.catid) AS item_count,   SUM(i.ownerid = @ownerid) AS owner_item_count FROM categories c   LEFT JOIN map m USING (catid)   LEFT JOIN items i USING (itemid) GROUP BY c.catid HAVING item_count > 0; 

    You can’t use column aliases in the WHERE clause, because expressions in the WHERE clause are evaluated before the expressions in the select-list. In other words, the values associated with select-list expressions aren’t available yet.

    You can use column aliases in the GROUP BY, HAVING, and ORDER BY clauses. These clauses are run after all the expressions in the select-list have been evaluated.

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

Sidebar

Related Questions

Here at work, we often need to find a string from the list of
When I am writing code in Python, I often need to remove items from
I often need to run reduce (also called foldl / foldr, depending on your
I often need to design a dialog in Delphi/C++Builder that allows various properties of
I have two scripts that often need to be run with the same parameter:
In php, I often need to map a variable using an array ... but
While developing products, we often need to create proprietary tools to test some of
My website makes a lot of requests. I often need to cancel all current
Often we need to add a non-nullable column to a table, and it is
I do scientific programming, and often want to show users prompts and variable pairs,

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.