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

时间:2017-05-14 05:24:49

标签: mysql group-concat

对于我的"旅行优惠,我有一个很大的问题。项目,工作率99%,但不是100%。

我有主要表格,其中每个优惠可以设置多个部门城市以及多个目的地城市(这是减少了列的减少样本)。

例如,我提供一些来自英格兰的旅行,其中部门城市可以来自伦敦,利兹和曼彻斯特。目的地城市是布拉格,布拉迪斯拉发,布达佩斯和贝尔格莱德。

优惠1设置为伦敦或利兹的百货公司,目的地是布拉格和布达佩斯。 优惠2设置为伦敦百货公司,目的地是布拉迪斯拉发和贝尔格莱德。 优惠3设置为部门城市曼彻斯特或利兹,目的地是布拉格。

Table offers
----------------------------
id  title            price
----------------------------
1   Offer 1 title    300 Eur
2   Offer 2 title    250 Eur
3   Offer 3 title    350 Eur

现在关系表和城市名称表

Table departments
----------------------------
id  name
----------------------------
1   London
2   Leeds
3   Manchester


relation Table rel_departments
------------------------
offer_id  rel_id
------------------------
1         1
1         2
2         1
3         2
3         3


Table destinations
----------------------------
id  name
----------------------------
1   Prague
2   Bratislava
3   Budapest
4   Belgrade


relation Table rel_destinations
------------------------
offer_id  rel_id
------------------------
1         1
1         3
2         2
2         4
3         1

作为SQL结果,我期望为部门城市和目的地城市的每个优惠连接值机器人

如果我用以下sql搜索所有内容我得到了OK结果:

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`)
GROUP BY offers.id

结果没问题:

---------------------------------------------------------------------
id  title            price    depCities          destCities
---------------------------------------------------------------------
1   Offer 1 title    300 Eur  London, Leeds      Prague, Budapest
2   Offer 2 title    250 Eur  London             Bratislava, Belgrade
3   Offer 3 title    350 Eur  Leeds, Manchester  Prague

无论WHERE子句是什么,我都需要这样的结果。但是,每当我放置where子句时,我就会松开连接中的一个结果。例如,我搜索布拉格作为目的地的所有优惠。如果我添加到sql语句的末尾:

where rel_destinations.rel_id=1

结果如下:

---------------------------------------------------------------------
id  title            price    depCities          destCities
---------------------------------------------------------------------
1   Offer 1 title    300 Eur  London, Leeds      Prague
3   Offer 3 title    350 Eur  Leeds, Manchester  Prague

如果您注意到,提供的布达佩斯没有1.如何获得完整的连接字符串...不是WHERE子句可以更复杂,即搜索部门城市或任何其他参数。

感谢任何帮助:)

1 个答案:

答案 0 :(得分:1)

您需要使用与rel_destinations不同的联接来获取布拉格作为目的地的优惠。加入原始查询。

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

DEMO