且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在单个mysql查询中从多个关系表中多个连接值

更新时间:2022-12-11 19:36:41

您需要将rel_destinations与其他联接一起使用,才能将布拉格作为目的地.将其与您的原始查询联系起来.

You need to use a different join with rel_destinations to get the offers with Prague as a destination. Join this with your original query.

SELECT offers.*, 
  GROUP_CONCAT(DISTINCT DEPC.name SEPARATOR ', ') AS depCities,
  GROUP_CONCAT(DISTINCT DESTC.name SEPARATOR ', ') AS destCities
FROM offers
INNER JOIN `rel_departments` ON (`rel_departments`.`offer_id` = `offers`.`id`)
INNER JOIN `departments` as DEPC ON (DEPC.`id` = `rel_departments`.`rel_id`)
INNER JOIN `rel_destinations` ON (`rel_destinations`.`offer_id` = `offers`.`id`)
INNER JOIN `destinations` as DESTC ON (DESTC.`id` = `rel_destinations`.`rel_id`)
INNER JOIN rel_destinations AS d1 ON d1.offer_id = offers.id
WHERE d1.rel_id = 1
GROUP BY offers.id

演示