I have a tag field for a blog posts. tags have unique id but their displayName might be duplicated. What I want is a query that selects posts and in all_tags
field we get couples of (id,displayName) is this way:
id1,name1;id2,name2;id3,name3
My query looks like:
select .... CONCAT_WS(';', DISTINCT (CONCAT_WS(',',tags.id,tags.displayName))) AS all_tags Join ...post content ... Join ...post_tags ... Join ...tags ... ORDER BY posts.id
This line causes problem:
CONCAT_WS(';', DISTINCT (CONCAT_WS(',',tags.id,tags.displayName))) AS all_tags
How should I modify it?
Some people use an inner (SELECT .. FROM)
but as I have heard, it is so inefficien
SELECT `posts`.*,`categories`.*,`creators`.*,`editors`.* CONCAT_WS(';', DISTINCT GROUP_CONCAT(CONCAT_WS(',',tags.id,tags.displayName))) AS all_ids FROM (`posts`) LEFT JOIN `languages` ON `posts`.`language_id`=`languages`.`id` LEFT JOIN `users` as creators ON `posts`.`creatorUser_id`=`creators`.`id` LEFT JOIN `users` as editors ON `posts`.`lastEditorUser_id`=`editors`.`id` LEFT JOIN `userProfiles` as editors_profile ON `editors`.`profile_id`=`editors_profile`.`id` LEFT JOIN `categories` ON `posts`.`category_id`=`categories`.`id` LEFT JOIN `postTags` ON `postTags`.`post_id`=`posts`.`id` LEFT JOIN `tags` ON `postTags`.`tag_id`=`tags`.`id` LEFT JOIN `postTags` as `nodetag_checks` ON `nodetag_checks`.`post_id`=`posts`.`id` LEFT JOIN `tags` as `tag_checks` ON `nodetag_checks`.`tag_id`=`tag_checks`.`id` WHERE ( 9 IN(`tag_checks`.`id`,`tag_checks`.`cached_parents`) OR 10 IN(`tag_checks`.`id`,`tag_checks`.`cached_parents`) OR 11 IN(`tag_checks`.`id`,`tag_checks`.`cached_parents`)) GROUP BY `posts`.`id` ORDER BY `posts`.`created` desc LIMIT 0, 20
Try this:
GROUP_CONCAT( DISTINCT CONCAT(tags.id,',',tags.displayName) ORDER BY posts.id SEPARATOR ';' )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With