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?