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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:48:22+00:00 2026-05-25T12:48:22+00:00

I’m trying to select items from an associative table that have satisfy two or

  • 0

I’m trying to select items from an associative table that have satisfy two or more values of the same field, sounds confusing, let me explain.

+-----------------------+
| item_id | category_id |
+-----------------------+
|   1     |    200      |
|   1     |    201      |
|   1     |    202      |
|   2     |    201      |
|   2     |    202      |
|   3     |    202      |
|   3     |    203      |
|   4     |    201      |
|   4     |    207      |
+-----------------------+

In the table, I want to be able to select only items which are in the categories that I pass. So for example, if I pass category IDs of 201 and 202, I would want only items that are in BOTH categories (they can have other categories but need to be in at least the categories I’m querying), so in this case, I would want items 1 and 2 only since they are the only ones that are in categories 201 and 202.

My initial SQL statement was something like

SELECT * FROM item_category WHERE category_id = 201 AND category_id = 202;

But obviously that won’t work.

SELECT * FROM item_category WHERE category_id = 201 OR category_id = 202;

The above query also won’t work because it’ll return items 4 and 3 as well.

So how would I go about only selecting items that have to at least be in both categories?

Keep in mind that I might be passing more than 2 category IDs.

Thank you for your help.

  • 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-25T12:48:22+00:00Added an answer on May 25, 2026 at 12:48 pm

    Your expression in a WHERE clause works against a single row of the joined result set. That’s why WHERE category_id = 201 AND category_id = 202 doesn’t work — because it can’t be two values on a single row.

    So you need some way to join two rows from the table into one row of the result set. You can do this with a self-join:

    SELECT c1.item_id
    FROM item_category AS c1
    INNER JOIN item_category AS c2 ON c1.item_id = c2.item_id
    WHERE c1.category_id = 201 AND c2.category_id = 202
    

    This technique is hard to scale up when you want to search for three, four, five or more values, because it requires N-1 joins to match N values.

    So another method is to use GROUP BY:

    SELECT c.item_id, COUNT(*) AS cat_count
    FROM item_category AS c
    WHERE c.category_id IN (201,202)
    GROUP BY c.item_id
    HAVING cat_count = 2
    

    Both techniques are okay, and work better in different circumstances.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

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.