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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:24:49+00:00 2026-05-31T23:24:49+00:00

I have three mysql tables items =========== id title items_in_categories ============================ id item_id category_id

  • 0

I have three mysql tables

items
===========
id    title

items_in_categories
============================
id    item_id    category_id

categories
===========
id    title

I want to find all the items that belong to ALL the stated categories. Not any one category, but ALL categories

Eg, if I want to search all the items that belongs to category id 3 and 5

the no. of possible categories to be searched can go up to as many as 20.

Eg, I want to get all the items that belongs to category id 1, 2, 3, 4, 5, 6, …. and 20

I would like to use as simple a way as possible.

I have tried AND and a nested NOT EXISTS as stated in the mysql manual.

Nothing worked.

UPDATE:
I came up with this.

select * from 
    (select count(*) as counter, item_id from items_in_categories 
        where category_id in (3, 5) group by item_id)getall 
where counter = 2

Is there a better way?

  • 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-31T23:24:51+00:00Added an answer on May 31, 2026 at 11:24 pm

    I know this can be prettied up and put into one query (You could nest the category count within the having instead of as a separate query…I just think this is more readable), but here is my shot at this:

    DECLARE @CategoryCount INT;
    
    SELECT @CategoryCount = COUNT(DISTINCT id) AS CategoryCount
    FROM categories
    WHERE category_id IN (3,5);
    
    SELECT *
    FROM items
    WHERE item_id IN 
    (
        SELECT item_id
        FROM items_in_categories
        WHERE items_in_categories.category_id IN (3,5)
        GROUP BY item_id
        HAVING COUNT(DISTINCT category_id) = @CategoryCount
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL query that: gets data from three tables linked by unique
I have 5 tables: library_item ============ id title description index_text library_item_rel_category ========================= item_id category_id
Using MySQL, I have three tables: projects : ID name 1 birthday party 2
I have a MySQL Left Join problem. I have three tables which I'm trying
I need help with a MySQL query. I have three tables: `product_category` ( `id`
I have a MySQL database with tables for Items and for Stores , and
I have two MySQL database tables that are meant to hold data for eshop
I have two MySQL tables: One for items and another to log purchases. I'm
i have a problem with joining three tables in mysql. lets say we have
I have MySQL database with two tables: users and items. They look 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.