很长的SQL调用中有很多JOIN

时间:2013-04-18 08:15:19

标签: mysql sql

有4个表,每个表都是一个元组ID(INT),name(VARCHAR)(ID是自动递增的)。有一张餐桌(芝士汉堡,汉堡包,肉宴,BLT,番茄汤),类(汉堡,比萨饼,三明治,汤),特色(香辣,素食,不含麸质)和双面(沙拉,薯条,面包)

还有三个表来制作元组Meal.ID,Class.IDMeal.ID,Characteristics.IDMeal.ID,Sides.ID,以便一个餐可以在多个班级中,有多个特征,并且有更多而不是一方,反之亦然。

我有下面的延迟加载就好了,但我们都知道这不好。我想要的是这顿饭的名称,它的特点,方面和来自同一个班级的替代餐点。

此调用工作正常:

SELECT Group_concat(DISTINCT m2.meal_name SEPARATOR ',')          AS alternates,
       Group_concat(DISTINCT c.characteristic_desc SEPARATOR ',') AS
       characteristics
FROM   meal AS M
       INNER JOIN mealclass AS MC
               ON M.meal_id = MC.meal_id
       LEFT JOIN (SELECT meal_id,
                         class_id
                  FROM   drugingredient) AS MC2
              ON MC2.class_id = MC.class_id
       LEFT JOIN meals AS M2
              ON MC2.meal_id = M2.meal_id
       LEFT JOIN mealchar AS MCh
              ON MCh.meal_id = M.meal_id
       INNER JOIN characterisics AS C
               ON C.characteristic_id = MCh.characteristic_id
WHERE  M.meal_id = :meal_id
       AND M.meal_id <> M2.meal_id  

但是当我把它扩展到这个时,我什么都没有回来:

SELECT Group_concat(DISTINCT m2.meal_name SEPARATOR ',')          AS alternates,
       Group_concat(DISTINCT c.characteristic_desc SEPARATOR ',') AS
       characteristics,
       Group_concat(DISTINCT s.sides_desc SEPARATOR ',')          AS side_orders
FROM   meal AS M
       INNER JOIN mealclass AS MC
               ON M.meal_id = MC.meal_id
       LEFT JOIN (SELECT meal_id,
                         class_id
                  FROM   drugingredient) AS MC2
              ON MC2.class_id = MC.class_id
       LEFT JOIN meals AS M2
              ON MC2.meal_id = M2.meal_id
       LEFT JOIN mealchar AS MCh
              ON MCh.meal_id = M.meal_id
       INNER JOIN characterisics AS C
               ON C.characteristic_id = MCh.characteristic_id
       INNER JOIN mealsides AS MS
               ON M.meal_id = MS.meal_id
       INNER JOIN sides AS S
               ON S.sides_id = MS.sides_id
WHERE  M.meal_id = :meal_id
       AND M.meal_id <> M2.meal_id 

任何想法如何修复调用,或如何拥有更好的结构化架构?或者是延迟加载的最佳方式(:P)?!

1 个答案:

答案 0 :(得分:1)

您想在这里做三件事。我把它们写成了示范。

create table a (x integer, y integer);
create table b (v integer, w integer);
create table c (t integer, u integer);
insert into a values (1,2);
insert into a values (3,4);
insert into a values (5,6);
insert into b values (1,2);
insert into b values (5,6);
insert into c values (1,2);
insert into c values (3,4);

这与您使用的相同:
 (左边加入b)加入c

select * from a left join b on a.x = b.v join c on b.v = c.t;
 x | y | v | w | t | u
---+---+---+---+---+---
 1 | 2 | 1 | 2 | 1 | 2

这将使用左连接:
 (左边加入b)左边加入c

select * from a left join b on a.x = b.v left join c on b.v = c.t;
 x | y | v | w | t | u
---+---+---+---+---+---
 1 | 2 | 1 | 2 | 1 | 2
 3 | 4 |   |   |   |
 5 | 6 | 5 | 6 |   |

认为这就是你的目标:  左边加入(b加入c)

select * from a left join (b join c on b.v = c.t) on a.x = b.v;
 x | y | v | w | t | u
---+---+---+---+---+---
 1 | 2 | 1 | 2 | 1 | 2
 3 | 4 |   |   |   |
 5 | 6 |   |   |   |

希望这有帮助