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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:22:37+00:00 2026-05-11T10:22:37+00:00

Following a previous question ( How to do this query in Mysql? ) Lets

  • 0

Following a previous question ( How to do this query in Mysql? )

Lets say I have a message:

Id: 1, Message: This is a message 

2 subjects:

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

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 Id: 3, message_id: 1, subject_id: 3 

I wanted to do a query to select the messages that had Math AND Science as subjects I ended up using:

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'); 

Now, its very clear that i would like to show the message(because it does have those 2 subjects) and also tell the user that it doesn’t ONLY have those 2 subject, but it actually have 3, and not only that… i would like to print the 3 subjects(Of course i have lots of messages and would like to actually list them, with their respective subjects in just 1 query). I cant seem to understand if that is actually possible with just one query, since i would get n ‘subject’ field as a return.

Any idea if this can be done with just one query and if so, how?

TY

  • 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-11T10:22:37+00:00Added an answer on May 11, 2026 at 10:22 am

    See GROUP_CONCAT for one solution. I don’t know how efficient this approach is, however.

    select message.Id as id, Message, group_concat(Subject separator ';') as subjects from message,subject,message_subject_rel where message.Id=message_subject_rel.message_id   and subject.Id=message_subject_rel.subject_id group by message.Id having subjects like '%Math%' and subjects like '%Science%'; 

    From that, I get:

    +------+---------+----------------------+ | id   | Message | subjects             | +------+---------+----------------------+ |    1 | Message | Math;Science;Numbers |  +------+---------+----------------------+ 

    Obviously, you could do a better job than my ‘;’ separator and my like clauses.

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

Sidebar

Related Questions

Following on from my previous question [link text][1] , I have a new problem.
Following on from my previous question , is it possible to make a Python
In a comment on a previous question, someone said that the following sql statement
Following this question: Good crash reporting library in c# Is there any library like
Following on from this question what would be the best way to write a
Following up on this question, I'm working on a large Delphi 7 codebase which
( NOTE: This is a follow up to a previous question, How to pass
While I'm still struggling to find a solution for this question, i have another
This is a follow-up to my previous question on pretty-printing STL containers , for
So, this is something of a followup to my previous question. I'm running into

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.