1

有 4 个表,每个表都是一个元组ID(INT),name(VARCHAR)(ID 是自动递增的)。餐(芝士汉堡、汉堡包、肉宴、BLT、番茄汤)、班级(汉堡、比萨、三明治、汤)、特色(辣、素食、无麸质)和配菜(沙拉、薯条、面包)有一张桌子.

存在另外三个表来制作元组Meal.ID,Class.ID, Meal.ID,Characteristics.IDMeal.ID,Sides.ID因此 Meal 可以属于多个类,具有多个特征,并且具有多个边,反之亦然。

我有以下延迟加载就好了,但我们都知道那不好。我想要的是这顿饭的名字,它的特点、配菜和同一班的替代餐。

这个电话工作得很好:

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)?!

4

1 回答 1

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);

这与您使用的相同:
(a LEFT JOIN b) JOIN 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

这将只使用左连接:
(a LEFT JOIN b) LEFT JOIN 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 |   |

认为这就是您的目标:a LEFT JOIN (b JOIN 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 |   |   |   |

希望这可以帮助

于 2013-04-18T08:48:03.673 回答