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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:22:41+00:00 2026-06-13T11:22:41+00:00

Apologies if this has been asked before but I really didn’t know what to

  • 0

Apologies if this has been asked before but I really didn’t know what to search and thus not sure how to word the question title so fingers crossed.

I’m building quite a common system where one product can have multiple categories and on the front-end, the user can search for products specifying multiple categories.

Say we had the following schema and data ( I cant post images yet, apologies, please see link )-

table name: product_categories

Data:

enter image description here

The products table is very standard – id, name, amount etc.

So product 1 has 2 categories.

If the user checks 2 tickboxes on the search page which represent category 2 and 3, how would I query that so only products which have both of those categories come back?

Something such as follows doesn’t work, as there is no one row in that which has a category id of 2 AND a category id of 3:

SELECT * from product_categories where `category_id` = 2 AND `category_id` = 3;

I’ve tried using WHERE IN aswell, however, this would return product 1 even if i was looking for products which had both category id 2 and 6 ( for example ) which I don’t want.

This is part of a bigger query however, I will be able to apply the solution if I manage to find one.

  • 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-13T11:22:42+00:00Added an answer on June 13, 2026 at 11:22 am

    Many thanks for the suggestions guys, much appreciated. I think found a solution, this seems to produce the expected results but I’ll be writing some more Unit Tests around my method to ensure it works.

    The solution I found is as follows:

    select * from products_categories where category_id in (2,3) HAVING count(distinct(id)) = 2 
    

    So this uses WHERE IN, however, using HAVING, it then checks the count of unique records returned and ensures it’s equal to the amount of categories provided.

    Cheers,
    Darren

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

Sidebar

Related Questions

I'm sure this question has been asked before, my apologies for not finding it
Apologies if this has been asked before (I couldn't find the answer anywhere), but
My sincere apologies if this question has been asked, but I really couldn't find
Apologies if this has been asked before but it seems an obvious question for
Apologies if this has been asked before but is there any way, at all,
My apologies if this has been asked before, I wasnt quite sure if this
I apologize if this has been asked before. I searched but did not find
(Apologies if this has been asked before - I can't believe it hasn't, but
apologies if this has been asked before but I have searched the site... Anyway,
First off, apologies if this question has been asked before but I couldn't find

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.