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 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

Ask A Question

Stats

  • Questions 104k
  • Answers 104k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer John Topley's answer above is basically correct, however since you… May 11, 2026 at 8:32 pm
  • Editorial Team
    Editorial Team added an answer HyperTerminal can be used to communicate over/read from a serial… May 11, 2026 at 8:32 pm
  • Editorial Team
    Editorial Team added an answer No, they're not guaranteed to be ordered. You might happen… May 11, 2026 at 8:32 pm

Related Questions

When importing numbers from a csv file, I need to convert them to floats
Thanks to Insin for answering a previous question related to this one. His answer
I have an SSIS package that does the following: Selects the connection strings from
In a comment on a previous question, someone said that the following sql statement

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.