반응형
블로그 게시물에 대한 태그 필드가 있습니다. 태그에는 고유 한 ID가 있지만 해당 displayName이 중복 될 수 있습니다. 내가 원하는 것은 게시물을 선택하는 쿼리이며 all_tags
필드에서 몇 개의 (id, displayName)을 얻는 방법은 다음과 같습니다.
id1,name1;id2,name2;id3,name3
내 쿼리는 다음과 같습니다.
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
이 줄은 문제를 일으 킵니다.
CONCAT_WS(';', DISTINCT (CONCAT_WS(',',tags.id,tags.displayName))) AS all_tags
어떻게 수정해야합니까?
어떤 사람들은 내부 (SELECT .. FROM)
를 사용하지만 제가 들었던 것처럼 너무 비효율적입니다
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
해결 방법
이 시도:
GROUP_CONCAT(
DISTINCT CONCAT(tags.id,',',tags.displayName)
ORDER BY posts.id
SEPARATOR ';'
)
참조 페이지 https://stackoverflow.com/questions/18845170
반응형
'MySql' 카테고리의 다른 글
MySQL mySQL에서 "행 ID"로 정렬 된 SELECT 결과 가져 오기 (0) | 2020.12.22 |
---|---|
MySQL mysql 테이블의 여러 외래 키를 동일한 기본 키로 (0) | 2020.12.22 |
MySQL VARCHAR과 CHAR의 차이점은 무엇입니까? (0) | 2020.12.22 |
MySQL Facebook 좋아요 알림 추적 (DB 디자인) (0) | 2020.12.22 |
MySQL for Python 2.7 (0) | 2020.12.22 |
댓글