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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:40:40+00:00 2026-05-13T08:40:40+00:00

I have a big mysql query which needs to trawl through 4 tables to

  • 0

I have a big mysql query which needs to trawl through 4 tables to get all the ‘items’ for my application. Each item can have many categories and each user can have up to one of each item. The items and categories are easy:

SELECT Items.itemId, Items.name, Items.type, Categories.name AS category 
FROM Items 
LEFT JOIN ItemCategories ON Items.itemId = ItemCategories.itemId
LEFT JOIN Categories ON ItemCategories.categoryId = Categories.categoryId;

This produces most of the data I want. However I also need to know if each item is owned by a particular user. So I simply added another join and a boolean column:

SELECT Items.itemId, Items.name, Items.type, Categories.name AS category, 
UserItems.userId = 7654321 AS userHas FROM Items
LEFT JOIN ItemCategories ON Items.itemId = ItemCategories.itemId
LEFT JOIN Categories ON ItemCategories.categoryId = Categories.categoryId
LEFT JOIN UserItems ON Items.itemId = UserItems.itemId;

The problem with this is that it will produce a LOT of unwanted results. For example if I have 500 users and each has 50 items then there would be 25,000 rows. Since there are only about 100 items in the database and each has approximately 3 categories I only really need about 300 rows. Basically I don’t need to know about items that other users might have, only the particular user I’m interested in.

My next step was to try grouping the rows:

GROUP BY Items.itemId, Categories.name

However this doesn’t ensure that my user’s data in the userHas column is preserved. I tried to SORT BY userHas DESC, but that seems to be applied after the GROUP BY.

I have a feeling that the solution might involve one of these functions: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
but I’m stuck as to what or how. I am aware that it might not even be possible in one query.

Here’s some output (not using GROUP BY):

+--------+--------+-----------+----------+---------+
| itemId | name   | type      | category | userHas |
+--------+--------+-----------+----------+---------+
|      1 | Llama  | character | animal   |    NULL |
|      1 | Llama  | character | nice     |    NULL |
|      2 | Slug   | character | animal   |       0 |
|      2 | Slug   | character | animal   |       1 |
|      2 | Slug   | character | nasty    |       0 |
|      2 | Slug   | character | nasty    |       1 |
|      3 | Sloth  | character | animal   |       1 |
|      3 | Sloth  | character | animal   |       0 |
|      4 | Banana | character | fruit    |       0 |
|      4 | Banana | character | animal   |       0 |
+--------+--------+-----------+----------+---------+

I want each item id, name, type all the categories and whether the user has one. The same data set With GROUP BY Items.itemId, Categories.name looks like:

+--------+--------+-----------+----------+---------+
| itemId | name   | type      | category | userHas |
+--------+--------+-----------+----------+---------+
|      1 | Llama  | character | animal   |    NULL |
|      1 | Llama  | character | nice     |    NULL |
|      2 | Slug   | character | animal   |       0 |
|      2 | Slug   | character | nasty    |       0 |
|      3 | Sloth  | character | animal   |       1 |
|      4 | Banana | character | animal   |       0 |
|      4 | Banana | character | fruit    |       0 |
+--------+--------+-----------+----------+---------+

The userHas = 1 field for Slug has been lost in the GROUP BY. I want to save it.

  • 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-13T08:40:40+00:00Added an answer on May 13, 2026 at 8:40 am

    Update:

    This will select all items and show if the user 7654321 has them.

    SELECT  Items.itemId, Items.name, Items.type, Categories.name AS category,
            NOT(ISNULL(UserID)) AS userHas
    FROM    Items
    LEFT JOIN
            ItemCategories
    ON      Items.itemId = ItemCategories.itemId
    LEFT JOIN
            Categories
    ON      ItemCategories.categoryId = Categories.categoryId
    LEFT JOIN
            UserItems
    ON      UserItems.itemId = Items.itemID
            AND UserItems.userId = 7654321
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL table which has a product_id field (big integer) 1102330008 1102330025
I have a big problem with my server mysql. All worked fine but since
I have a problem with mysql query. I am writing SQL query to get
I have a big MySQL users table and need to get six random rows
I have big URL list, which I have to download in parallel and check
Hi I have one big problem with one MYSQL search. My database TABLE looks
I have MySQL database, where I store the following BLOB (which contains JSON object)
Mysql begin generate big IO. One website have many rows in session,requests table (
I have a pretty big query to pull out the information about reports from
I have a MySQL table , each row representing a Person. For each person

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.