加入,從上entry.id加入下面的表格,我想提取從food_brands表具有最高TYPE_ID行 - 所以我應該得到下面的前3排,TYPE_ID 11940MySQL在最大值
food_brands
id brand type_id
15375 cesar 11940
15374 brunos 11940
15373 butchers 11940
15372 bakers 11939
15371 asda 11939
15370 aldi 11939
類型
id type quantity food_id
11940 comm 53453 10497
11939 comm 999 10496
食品
id frequency entry_id
10497 twice 12230
10496 twice 12230
10495 once 12230
項
id number
12230 26
我在查詢嘗試未過濾掉較低type.id記錄 - 從表中的記錄下方food_brands,我得到那些TYPE_ID 11940和11939.感謝這麼任何幫助解決此問題!
SELECT fb.*
FROM food_brands fb
INNER JOIN types t ON fb.type_id = t.id
INNER JOIN
(
SELECT MAX(id) AS MaxID
FROM types
GROUP BY id
) t2 ON t.food_id = t2.food_id AND t.id = t2.MaxID
INNER JOIN foods f ON t.food_id = f.id
INNER JOIN entries e ON f.entry_id = e.id
WHERE entries.id = 12230
是什麼'?feeding_id' – didierc 2013-03-27 18:33:27
didierc - 斑點!更新 – bsod99 2013-03-28 10:21:43