【发布时间】:2020-08-04 16:42:32
【问题描述】:
问题:
由于我要连接两个表 - prices 和 videos,GROUP_CONCAT() 会为第二个 LEFT JOIN 连接的每一行重复值。
我的尝试:
SELECT
`Cat`.*,
GROUP_CONCAT(COALESCE(`Price`.`id`, "") ORDER BY `Price`.`price`) AS `PriceId`,
GROUP_CONCAT(COALESCE(`Price`.`price`, "") ORDER BY `Price`.`price`) AS `PricePrice`,
GROUP_CONCAT(COALESCE(`Vid`.`id`, "") ORDER BY `Vid`.`id`) AS `VideoId`,
GROUP_CONCAT(COALESCE(`Vid`.`uuid`, "") ORDER BY `Vid`.`id`) AS `VideoUUID`
FROM
`categories` AS `Cat`
LEFT JOIN `prices` AS `Price` ON `Cat`.`id`=`Price`.`category_id`
LEFT JOIN `videos` AS `Vid` ON `Cat`.`id`=`Vid`.`category_id`
GROUP BY
`Cat`.`id`
问题:
如何调整查询,使 SQLFiddle 输出中的 PricePrice、VideoId、VideoUUID 列不包含重复项?
我确实尝试在GROUP_CONCAT 中添加DISTINCT,但这无济于事,因为它会过滤掉我应该保留的重复值(例如,price)
谢谢!
【问题讨论】: