mysql - Many JOINs in a long SQL call -
there 4 tables, each tuple id(int),name(varchar)
(the id autoincrementing). there table meal (cheeseburger, hamburger, meat feast, blt, tomato soup), class (burger, pizza, sandwich, soup), characteristics (spicy, vegtarian, gluten free), , sides(salad, fries, bread).
three more tables exist make tuples meal.id,class.id
, meal.id,characteristics.id
, meal.id,sides.id
meal can in more 1 class, have more 1 characteristic, , come more 1 side, , vice versa.
i had following lazy loading fine, know that's not good. want name of meal, it's characteristics, sides , alternative meals same class.
this call worked fine:
select group_concat(distinct m2.meal_name separator ',') alternates, group_concat(distinct c.characteristic_desc separator ',') characteristics meal m inner join mealclass mc on m.meal_id = mc.meal_id left join (select meal_id, class_id drugingredient) mc2 on mc2.class_id = mc.class_id left join meals m2 on mc2.meal_id = m2.meal_id left join mealchar mch on mch.meal_id = m.meal_id inner join characterisics c on c.characteristic_id = mch.characteristic_id m.meal_id = :meal_id , m.meal_id <> m2.meal_id
but when extended out this, got nothing back:
select group_concat(distinct m2.meal_name separator ',') alternates, group_concat(distinct c.characteristic_desc separator ',') characteristics, group_concat(distinct s.sides_desc separator ',') side_orders meal m inner join mealclass mc on m.meal_id = mc.meal_id left join (select meal_id, class_id drugingredient) mc2 on mc2.class_id = mc.class_id left join meals m2 on mc2.meal_id = m2.meal_id left join mealchar mch on mch.meal_id = m.meal_id inner join characterisics c on c.characteristic_id = mch.characteristic_id inner join mealsides ms on m.meal_id = ms.meal_id inner join sides s on s.sides_id = ms.sides_id m.meal_id = :meal_id , m.meal_id <> m2.meal_id
any ideas how fix call, or how have better structured schema? or degree of lazy loading best way go here (:p)?!
there 3 possible things here. i've written them out demonstration.
create table (x integer, y integer); create table b (v integer, w integer); create table c (t integer, u integer); insert values (1,2); insert values (3,4); insert values (5,6); insert b values (1,2); insert b values (5,6); insert c values (1,2); insert c values (3,4);
this same used:
(a left join b) join c
select * 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
this using left joins:
(a left join b) left join c
select * 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 | |
i think aiming for: left join (b join c)
select * 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 | | | |
hope helps
Comments
Post a Comment