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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:51:38+00:00 2026-05-27T12:51:38+00:00

I have a table of items and I have a separate table containing the

  • 0

I have a table of items and I have a separate table containing the individual purchases of each item. Not every item has been purchased. I am selecting the purchase count and joining it with the items table with a statement similar to the following:

SELECT items.name, purchases_count
FROM `items` 
    LEFT JOIN (SELECT sales.item_name, SUM(unit_sales) AS purchases_count 
        FROM sales GROUP BY item_name) sales ON sales.item_name = items.uid

This results in data similar to:

+------+-----------------+
| name | purchases_count |
|------+-----------------+
| Shoe | 12              |
| Belt | NULL            |
+------+-----------------+

My problem arises when I want to filter this result on the purchases_count. If I use WHERE purchases_count < 10 or something similar, items with no sales record at all (i.e. those with a NULL value) are excluded from the search.

I have attempted to use COALESCE(purchases_count,0) AS purchases_count which does correctly set NULL records to 0, but they are still not appearing when using WHERE. I suspect the COALESCE is happening after the WHERE.

I would like to be able to filter on this number using WHERE and include NULL results (as zeros) if possible. Any suggestions?

  • 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-27T12:51:39+00:00Added an answer on May 27, 2026 at 12:51 pm

    Aliases don’t generally apply in the WHERE clause – try COALESCE(purchases_count,0) AS purchases_count_2 with where purchases_count_2 < 10 to see what I mean.

    where COALESCE(purchases_count,0) < 10 should work.

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

Sidebar

Related Questions

I have a table of items, each of which has a date associated with
I have a table of items. item has id, score 1, score 2. I
I have a listing of items on a site. Each item has several categories
I have a database which has an orders table and inventory table. The order-items
I have a producer thread in Java pulling items from an Oracle table every
I have a table Users and a table Items In the Items table, I
I'm working on a very basic shopping cart system. I have a table items
I have a table of items, and a table of itemkeywords. When a user
I have a table of items stored this way : A1 | B1 A1
I have a table of items, user should be able to choose some of

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.