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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:05:28+00:00 2026-05-12T12:05:28+00:00

I have 3 tables (scenes, categories, scenes_categories ) in a many to many relationship.

  • 0

I have 3 tables (scenes, categories, scenes_categories ) in a many to many relationship.

scenes ( id, title, description )
categories ( id, title )
scenes_categories ( scene_id, category_id )

I’m having problems making a query to select scenes that must match multiple categories. For example, I might want to select scenes that match category 3 AND category 5 AND category 8, but I can’t figure out how to get this to work.

So far I’ve got something like

SELECT scenes.id, scenes.title, scenes.description
FROM scenes
LEFT JOIN scenes_categories ON scenes.id = scenes_categories.scene_id
LEFT JOIN categories ON scenes_categories.category_id = categories.id
WHERE scenes_categories.category_id = '3'
AND scenes_categories.category_id = '5'
AND scenes_categories.category_id = '8'
AND scenes.id = '1'

How can I select for records that must match all the category ID’s specified?

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

    You need to require that a row exists in your many-to-many table for that sceneId, for each categoryId you are requiring:
    So try this:

    SELECT s.id, s.title, s.description
    FROM scenes s
    WHERE s.id = '1'
       And Exists (Select * From scenes_categories 
                   Where scene_id = s.Id
                      And category_id = '3')
       And Exists (Select * From scenes_categories 
                   Where scene_id = s.Id
                      And category_id = '5')
    
       And Exists (Select * From scenes_categories 
                   Where scene_id = s.Id
                      And category_id = '8')
    

    another option that should work is to do three inner joins instead:

    SELECT s.id, s.title, s.description
    FROM scenes s
      Join scenes_categories c3 
          On c3.scene_id  = s.Id
               And c3.category_id ='3'
      Join scenes_categories c5 
          On c5.scene_id  = s.Id
               And c5.category_id ='5'
      Join scenes_categories c8 
          On c8.scene_id  = s.Id
               And c8.category_id ='8'     
    WHERE s.id = '1'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tables linked by FK, I query on the first table using entity
hi i have tables like this: Persons: person_id, name then i have many langauge
I have tables A and B with identity PKs ida and idb : ex
I have tables like these two test_table date student test 2012-05-31 Alice Math 2012-05-31
I have tables like this: tblUsers int UserID string UserName tblUsersInRoles int UserID int
I have tables named news and tags_news. Given a news id I have to
I have tables Product ( ID, CategoryID, ... ) Category ( ID, Name, ..
In the database we have tables called Sites, Organisations, Person, Department have the same
I used to have tables before to display the content and people here advised
I have an application in mind which dicates database tables be append-only; that is,

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.