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

  • Home
  • SEARCH
  • 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 8137891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:19:04+00:00 2026-06-06T11:19:04+00:00

I have 2 tables: items and items_purchased . The tag name and other descriptive

  • 0

I have 2 tables: items and items_purchased. The tag name and other descriptive information is in the items table and the # of purchases for each item is saved in the items_purchased table. The tables are JOINed on item_id.

Here’s my query so far:

(select *, sum(purchaseyesno) as tots from items join items_purchased on items.item_id=items_purchased.item_id where tag = 'History' and deleted!=1 and pub_priv!=1    order by tots desc limit 1)
UNION ALL
(select *, sum(purchaseyesno) as tots from items join items_purchased on items.item_id=items_purchased.item_id where tag = 'Medicine' and deleted!=1 and pub_priv!=1  order by tots desc limit 1)
UNION ALL
(select *, sum(purchaseyesno) as tots from items join items_purchased on items.item_id=items_purchased.item_id where tag = 'Biology' and deleted!=1 and pub_priv!=1  order by tots desc limit 1)
//... more tags (20 in all) would follow

The problem is that items with certain tags haven’t been purchased yet so the query throws an error of Column "item_id" cannot be null.

here’s the sample tables:

            items              |     items_purchased   |   
  item_id   tag      title     | item_id  purchaseyesno|
    1     Biology  DNA is cool |    1         1        | 
    2     Medicine  Doctors    |    2         1        |  
    3      Law    Laws are cool|    4         1        |    
    4     Biology DNA NOT cool |    1         1        |

Sample results:

item_id   tag       title      tots
  1      Biology   DNA is cool  2
  2      Medicine  Doctors      1
  3       Law   Laws are cool   0

2 questions:

  1. How can I exclude one of these SELECT statements if the results of it will be NULL?
  2. Is there a better way to do this query rather than 20 select statements joined by 19 UNION ALLs?
  • 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-06T11:19:06+00:00Added an answer on June 6, 2026 at 11:19 am

    You simply want to group your results—nothing more complicated than that (no UNION in sight!):

    SELECT   items.*, SUM(purchaseyesno) AS tots
    FROM     items JOIN items_purchased USING (item_id)
    GROUP BY item_id
    

    UPDATE

    Following the comments beneath and the update to the OP’s question, it’s clear that the problem is more complicated than first thought: it is, in fact, a special case of the group-wise maximum problem.

    However, the column over which the maximum must be taken is itself the result of another group-wise aggregation (the summation of purchaseyesno).

    Therefore, one’s query becomes rather inelegant with subqueries:

    SELECT *
    FROM (
      SELECT   items.*, IFNULL(SUM(purchaseyesno),0) AS tots
      FROM     items LEFT JOIN items_purchased ON folder_id = item_id
      GROUP BY item_id
    ) AS t NATURAL JOIN (
      SELECT tag, MAX(tots) AS tots
      FROM (
        SELECT   items.*, IFNULL(SUM(purchaseyesno),0) AS tots
        FROM     items LEFT JOIN items_purchased ON folder_id = item_id
        GROUP BY item_id
      ) AS u
      GROUP BY tag
    ) AS v
    GROUP BY tag
    

    See it on sqlfiddle.

    What’s happening here is that (using an outer join to include records such as Law that have no references in the items_purchased table), we take the sum of purchases for each item (materialised table u) and then determine the maximum number of purchases for items with the same tag name.

    We then join the result (materialised table v) back with the first table (u, materialised again as table t) on both tag name and number of purchases.

    Finally we group the results again by tag to ensure that, if multiple items having the same tag have the same number of maximal purchases, only one of them is returned indeterminately.

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

Sidebar

Related Questions

I have two tables: Table items: | id | name | description | |
I have 2 tables: users and items. Each item has a user#. So, if
I have two mysql tables: Item containing items that one can buy: CREATE TABLE
I have 3 tables: items , purchases , and collaborators . A user can
I have two tables in a MySql DB: items id (char) name (varchar) description
I have three tables filters (id, name) items(item_id, name) items_filters(item_id, filter_id, value_id) values(id, filter_id,
I have 2 tables, one of them stores items and the other one stores
I have 3 tables in my DB (MySQL). categories (name:string) items (name:string, category_id:int) votes
I have a table containing transaction information on items. The possible transactions are purchase
I have a 'customers' table. Each record contains a customer name, email, address, etc.

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.