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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:04:19+00:00 2026-05-11T14:04:19+00:00

I have 3 tables, message, subject and message_subject_rel . The idea is to have

  • 0

I have 3 tables, message, subject and message_subject_rel. The idea is to have messages that can relate to a lot of subjects and then do a cross subject search.

Lets say I have a message:

Id: 1, Message: This is a message 

2 subjects:

Id:1, Subject: Math Id:2, Subject: Science 

And there’s 2 message_subject_rel entries that go:

Id: 1, message_id: 1, subject_id: 1 Id: 2, message_id: 1, subject_id: 2 

If i wanted to search the messages that are related with math, I would do a simple join with the 3 tables and the where clause would be subject = ‘Math’

But what I don’t know how to do, is search for the messages that are related with math AND Science. If i do a simple join i get tables with something like:

id  message     user_id     created_at  ip  id  message_id  subject_id  id  subject 

And if I do a where subject = 'Math' and subject = 'Science' i wont get any results because each message will only have 1 subject related in each row, but duplicated rows for messages with more than 1 subject.

So, what do you recommend?

  • 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. 2026-05-11T14:04:20+00:00Added an answer on May 11, 2026 at 2:04 pm

    This is basically the same question as Filtering from join-table

    I’ll adapt my answer from that question.

    JOIN solution:

    SELECT m.* FROM messages m  JOIN message_subject_rel ms1 ON (m.id = ms1.message_id)  JOIN subjects s1 ON (ms1.subject_id = s1.id AND s1.subject = 'Math')  JOIN message_subject_rel ms2 ON (m.id = ms1.message_id)  JOIN subjects s2 ON (ms2.subject_id = s2.id AND s2.subject = 'Science'); 

    GROUP BY solution:

    Note that you need to list all m.* columns in the GROUP BY clause, unless you use MySQL.

    SELECT m.* FROM messages m   JOIN message_subject_rel ms ON (m.id = ms.message_id)  JOIN subjects s ON (ms.subject_id = s.id) WHERE s.subject IN ('Math', 'Science')) GROUP BY m.id, ... HAVING COUNT(*) = 2; 

    Subquery solution:

    SELECT m.* FROM messages m WHERE m.id = ANY (SELECT message_id                    FROM message_subject_rel ms JOIN subjects s                      ON (ms.subject_id = s.id)                    WHERE s.subject = 'Math')   AND m.id = ANY (SELECT message_id                    FROM message_subject_rel ms JOIN subjects s                      ON (ms.subject_id = s.id)                    WHERE s.subject = 'Science'); 

    Modified GROUP BY solution:

    Simplifies GROUP BY clause by isolating search in a subquery.

    SELECT m.* FROM messages m WHERE m.id IN (   SELECT ms.message_id FROM message_subject_rel ms JOIN subjects s     ON (ms.subject_id = s.id)   WHERE s.subject IN ('Math', 'Science'))   GROUP BY ms.message_id HAVING COUNT(*) = 2 ); 

    PS: There’s no reason your message_subject_rel table needs an ID column.

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

Sidebar

Related Questions

I have 2 related tables: messages -------- mid subject --- ----------------- 1 Hello world
I have 2 tables containing information that I want to search for, a main
I have 3 tables; Inbox, Outbox, and Messages. For a single message, the data
I have form partial that allows the user to enter the subject and message
I feel totally stupid. I'm rusty with my sql. I have two tables, Message
I have table messages | id | user_id | recepient_id | message | 1
I have three tables: User: UserId (pk) FirstName Lastname Messages: MessageId (pk) Heading Body
I have a table row that has the error message in it. <tr runat=server
I have the following tables in my database: Minutes: minute_id, subject, next_subject, approval, meeting_id
Two tables. emails id (int10) | ownership (int10) messages emailid (int10) indexed | message

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.