有 4 个表,每个表都是一个元组ID(INT),name(VARCHAR)
(ID 是自动递增的)。餐(芝士汉堡、汉堡包、肉宴、BLT、番茄汤)、班级(汉堡、比萨、三明治、汤)、特色(辣、素食、无麸质)和配菜(沙拉、薯条、面包)有一张桌子.
存在另外三个表来制作元组Meal.ID,Class.ID
, Meal.ID,Characteristics.ID
,Meal.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)?!