经过多次传递后,我想知道是否可以进一步优化此查询?非常感谢任何见解。除了必须使用一堆不理想的子查询之外,我还必须使用其中一个两次,因为“标签”引用不能在 where 子句中使用......(无法弄清楚如何将此重写为连接以解决该问题)
SELECT `t1`.*,
(SELECT GROUP_CONCAT(tag_id) FROM media_tag WHERE media_id=t1.id GROUP BY media_id ) as tags,
(SELECT GROUP_CONCAT(award_id) FROM media_award WHERE media_id=t1.id GROUP BY media_id ) as awards,
(SELECT GROUP_CONCAT(client_id) FROM media_client WHERE media_id=t1.id GROUP BY media_id ) as clients,
(SELECT GROUP_CONCAT(collaborator_id) FROM media_collaborator t2 LEFT JOIN collaborator t3 ON t2.collaborator_id=t3.id WHERE media_id=t1.id AND t3.collaborator_type_id='1' GROUP BY media_id ) as agencies,
(SELECT GROUP_CONCAT(collaborator_id) FROM media_collaborator t2 LEFT JOIN collaborator t3 ON t2.collaborator_id=t3.id WHERE media_id=t1.id AND t3.collaborator_type_id<>'1' GROUP BY media_id ) as collaborators,
`t2`.`idx`, `t2`.`type`
FROM (`media` t1)
LEFT JOIN `sort` t2 ON `t1`.`id`=`t2`.`id` AND t2.type="media"
WHERE
FIND_IN_SET('1',(SELECT GROUP_CONCAT(tag_id) FROM media_tag WHERE media_id=t1.id GROUP BY media_id ))
AND IF(t2.tag_id IS NOT NULL,t2.tag_id='1',1)
GROUP BY `t1`.`id`
ORDER BY `t1`.`date_mod` DESC, `title` ASC, `t2`.`idx` ASC, `date_mod` DESC, `title` ASC