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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:02:05+00:00 2026-05-11T13:02:05+00:00

This is kinda weird. I have the next query: SELECT * , GROUP_CONCAT( x.tag

  • 0

This is kinda weird. I have the next query:

SELECT * , GROUP_CONCAT( x.tag SEPARATOR ',' ) AS tags FROM tag AS t, tag AS x, tag_message_rel AS r, message m INNER JOIN `user` AS u ON m.user_id = u.id WHERE t.tag IN ( 'kikikiki', 'dsa' ) AND m.id = r.message_id AND t.id = r.tag_id AND x.id = r.tag_id GROUP BY m.id HAVING COUNT( * ) >=2 ORDER BY m.created_at DESC LIMIT 0 , 20 

As you can see i use t to join find the messages that i want, on the other side i use x to print the tags of a message. I i erase the line:

AND x.id = r.tag_id 

I will get the messages that i want, but tags will have ALL the tags in the tags table separated by coma. If i leave the line there, i will only get those 2 tags. If i use explain i get:

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra 1   SIMPLE  u   system  PRIMARY     NULL    NULL    NULL    1   Using temporary; Using filesort 1   SIMPLE  t   range   PRIMARY,tag     tag     252     NULL    2   Using where 1   SIMPLE  x   eq_ref  PRIMARY     PRIMARY     4   verse.t.id  1     1   SIMPLE  r   ALL     NULL    NULL    NULL    NULL    180     Using where; Using join buffer 1   SIMPLE  m   eq_ref  PRIMARY     PRIMARY     4   verse.r.message_id  1   Using where 

Now im no expert in this, but i think the problem is that it is refusing to re-join a table in the process of optimizing the query.

What do you think? Any quick fix?

  • 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-11T13:02:06+00:00Added an answer on May 11, 2026 at 1:02 pm

    The problem is that you are trying to join to the tag table twice, but you really need to join to the tag_message_rel table twice, and from each of these to the respective row in the tag table.

    Think of ‘table aliases’ as referring to a row in a table, not the table itself. That idea helped me to understand complex joins a lot better.

    Here’s how I’d write that query:

    SELECT m.*, u.*, GROUP_CONCAT(DISTINCT x.tag) AS tags FROM message m  JOIN `user` u ON (u.id = m.user_id)  JOIN tag_message_rel r1 ON (m.id = r1.message_id)  JOIN tag t ON (t.id = r1.tag_id)  JOIN tag_message_rel r2 ON (m.id = r2.message_id)  JOIN tag x ON (x.id = r2.tag_id) WHERE t.tag IN ('kikikiki', 'dsa') GROUP BY m.id HAVING COUNT(DISTINCT t.tag) = 2 ORDER BY m.created_at DESC LIMIT 0 , 20; 

    You should develop the habit of using JOIN syntax consistently. Mixing JOIN and comma-style joins can cause some subtle problems.

    Here’s an alternative query, that pulls some of the joins into a non-correlated subquery, so you avoid a Cartesian product between t and x, and eliminate the DISTINCT modifiers in the group functions.

    SELECT m.*, u.*, GROUP_CONCAT(x.tag) AS tags FROM message m  JOIN `user` u ON (u.id = m.user_id)  JOIN tag_message_rel r ON (m.id = r.message_id)  JOIN tag x ON (x.id = r.tag_id) WHERE m.id = ANY (     SELECT m2.id      FROM message m2       JOIN tag_message_rel r2 ON (m2.id = r2.message_id)      JOIN tag t ON (t.id = r2.tag_id)     WHERE t.tag IN ('kikikiki', 'dsa')      GROUP BY m2.id      HAVING COUNT(t.tag) = 2) GROUP BY m.id ORDER BY m.created_at DESC LIMIT 0 , 20; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 244k
  • Answers 244k
  • 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 dojo.query() returns a NodeList. dojo.addClass() and the rest work with… May 13, 2026 at 8:04 am
  • Editorial Team
    Editorial Team added an answer The Project Folder is where the nbproject folder (Netbeans configuration… May 13, 2026 at 8:04 am
  • Editorial Team
    Editorial Team added an answer Who is doing the sending of the emails? From your… May 13, 2026 at 8:04 am

Related Questions

I have a UserControl that consists of three TextBoxes. On a form I can
Some users of our Swing application have reported weird artefacts appearing on the display.
I am trying to write a simple if statement in javascript (jquery library) that
I am using codeigniter's session class to handle my PHP sessions. One of the

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.