我有三個表,我想在MySQ中查詢。具體如下:MySQL從多個表中選擇多個GROUP BY和group_concat?
**Table: Leaderboard**
Name | Score
------------
James | 1
Steve | 2
Dave | 5
**Table: Actions**
Name | Action | Time
----------------------------
James | Ate an apple | 01:00
James | Kicked a dog | 02:00
Steve | Ate a dog | 03:00
Steve | Kicked a hen | 01:00
Dave | died | 02:00
**Table: Items**
Name | Item | Time
----------------------------
James | Chainsaw | 01:00
James | Hammer | 01:05
James | Crowbar | 01:10
Steve | Hammer | 02:00
Steve | Egg | 01:05
Dave | Egg | 01:05
我需要它選擇每個玩家(ORDER BY Leaderboard.score DESC),並選擇WHERE Actions.action LIKE '%吃' 自己的最新動作,然後給所有Items.Item ORDER BY查詢時間倒序
因此,舉例來說,輸出應該是這樣的
**Output**
Name | Latest_Action | Items
Steve | Ate a dog | Hammer, Egg
James | Ate an apple | Crowbar, Hammer, Chainsaw
到目前爲止,我曾嘗試以下查詢,但它多次返回每個項目在GROUP_CONCAT
SELECT Leaderboard.Name, Actions.*, group_concat(Items.Item)
FROM Leaderboard, Actions, Items
WHERE Items.Name = Actions.Name
AND Actions.Action LIKE 'Ate %'
AND Actions.Name IN (SELECT Name FROM Leaderboard ORDER BY SCORE DESC)
GROUP BY Leaderboard.name
任何幫助非常感謝!
您使用子查詢對結果進行排序將不起作用:外面的GROUP BY將覆蓋到目前爲止存在的任何排序。 – MvG 2012-07-10 14:26:31