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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:50:46+00:00 2026-06-15T20:50:46+00:00

I have a table with data like this: +———+———+ | user_id | item_id |

  • 0

I have a table with data like this:

+---------+---------+
| user_id | item_id |
+---------+---------+
|     125 |     375 |
|     125 |       2 |
|     125 |      12 |
|     125 |     375 |
|     283 |       2 |
|     283 |      11 |
|     118 |     375 |
|     118 |      12 |
+---------+---------+

And I’m trying to create a query that will return all users that have ALL of the requested items, which in this case the requested items are:

item_id IN (375, 12) 

So this should be the result:

+---------+
| user_id |
+---------+
|     125 |
|     118 |
+---------+

Short of joining the table to itself, I’m not really sure how I would accomplish this.
One idea I thought of was to group it by: user_id, item_id then maybe put a count on it, but that will count the double record of user_id: 125, item_id: 375 as 2, putting the total count for user_id 125 at 3, which has it’s own set of problems such as returning users that have the same item twice vs. what I really need, which is a user that has both of the requested items.

  • 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-15T20:50:48+00:00Added an answer on June 15, 2026 at 8:50 pm

    You could use HAVING COUNT(DISTINCT item_id) = 2, but I like to be more explicit—the following approach allows for arbitrary boolean logic:

    SELECT   user_id
    FROM     my_table
    WHERE    item_id IN (375, 12)
    GROUP BY user_id
    HAVING   SUM(item_id = 375) AND SUM(item_id = 12)
    

    See it on sqlfiddle.

    This works in MySQL because it does not have true boolean types; instead, the “boolean” result of the comparisons is in fact an integer—1 if true and 0 if false—therefore the summation over the group is zero if no match found and non-zero otherwise: such terms can be combined with logical operators due to MySQL’s implicit type conversion back to “boolean” types. In other RDBMS, one might have to use CASE:

    HAVING   SUM(CASE WHEN item_id = 375 THEN 1 ELSE 0 END) > 0
         AND SUM(CASE WHEN item_id =  12 THEN 1 ELSE 0 END) > 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I like to have a query like this: select data from table where (x
I have a table with data that looks like this: create table demo_patient_info (
In MSSQL 2008 I have table and data which looks like this create table
Need a query help. I have a table where data is represented like this
I have a table of data like this: id user_id A B C =====================
My table have data structure like this cate_id task_id date_start date_end other 34 14
I have a table like this: Application,Program,UsedObject It can have data like this: A,P1,ZZ
I have a table (call it table myTable). It has the data like this:
I have a data.table object like this one library(data.table) a <- structure(list(PERMNO = c(10006L,
I'm a bit confused on this. I have a data table structured like this:

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.