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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:04:54+00:00 2026-05-23T09:04:54+00:00

I have 3 tables. Tour Table (optional) Tour Tag Relation Table (optional_tag_rel) Tour Theme

  • 0

I have 3 tables.

  1. Tour Table (optional)
  2. Tour Tag Relation Table (optional_tag_rel)
  3. Tour Theme Relation Table (optional_theme_rel)

A tour can have multiple tags and multiple themes. So I what to write and sql statement to find specified tours. For example find tours where tag id = 10 and 15 and theme id = 36 and 45.

SELECT DISTINCT (t1.id) FROM optional AS t1 
LEFT JOIN optional_theme_rel as t3 ON t3.tour_id = t1.id AND (t3.theme_id =36 OR t3.theme_id =45)
LEFT JOIN optional_tag_rel as t2 ON t2.tour_id = t1.id AND (t2.tag_id = 10 OR t2.tag_id =15) 
GROUP BY optional.id 

These statement gets all records whose theme id = 36 or 45, but I need to use AND statement but when I use AND instead of OR, it shows no record. There has to be different way of doing this on sql but I dont know much about it.

  • 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-23T09:04:54+00:00Added an answer on May 23, 2026 at 9:04 am

    If I understand correctly that you want tours that have both themes (with theme_id =36 and 45) and both tags (with tag_id=10 and 15), then you neither need the GROUP BY nor the DISTINCT.

    But one way to do this is to join twice to each one of the secondary tables:

    SELECT op.id
    FROM optional AS op
    
      JOIN optional_theme_rel AS theme1
        ON theme1.tour_id = op.id
        AND theme1.theme_id = 36
      JOIN optional_theme_rel AS theme2
        ON theme2.tour_id = op.id
        AND theme2.theme_id = 45
    
      JOIN optional_tag_rel AS tag1
        ON tag1.tour_id = op.id
        AND tag1.tag_id = 10
      JOIN optional_tag_rel AS tag2
        ON tag2.tour_id = op.id
        AND tag2.tag_id = 15
    

    Another way to get same results is:

    SELECT op.id
    FROM optional AS op
    
      JOIN 
        ( SELECT tour_id
          FROM optional_theme_rel
          WHERE theme_id IN (36, 45)
          GROUP BY tour_id
          HAVING COUNT(DISTINCT theme_id) = 2
        ) AS theme
        ON theme.tour_id = op.id
    
      JOIN 
        ( SELECT tour_id
          FROM optional_tag_rel
          WHERE tag_id IN (10, 15)
          GROUP BY tour_id
          HAVING COUNT(DISTINCT tag_id) = 2
        ) AS tag
        ON tag.tour_id = op.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my database I have tables that define types for example Table: Publication Types
I have three tables tag , page , pagetag With the data below page
I have tables A, B, C, where A represents items which can have zero
I have tables: Teachers and Students. We have a SpecialProjects Table where Teachers and
I have tables item and store (it's a store management system). item table has
I have tables of data samples, with a timestamp and some data. Each table
I have tables named Events and Log. The Events table comprises of ID, Event
I have some simple tables (multiple, all with class=parent) with multiple <tr> rows. The
I have a query that pulls values from multiple tables. I want to order
I have two tables, say Table A and Table B. I want to have

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.