数据库表包含目标操作。
type goal assist
goal Johnny James
goal Johnny James
goal James Bob
使用GROUP BY goal, Assist时显示
player goals assists
Johnny 2 0
Johnny 0 0
James 1 0
James 0 2
Bob 0 0
Bob 0 1
但我需要它在一行中显示球员的进球和助攻。干杯。
最佳答案
您可以这样做(尽管这可能不是最快的查询,具体取决于数据库和索引的大小!):
SELECT players.player,
-- Use correlated subselects to count goals / assists
(SELECT COUNT(*) FROM actions WHERE goal = players.player) goals
(SELECT COUNT(*) FROM actions WHERE assist = players.player) assists
-- Get all distinct players (UNION = DISTINCT, here). Of course, if you
-- have an actual players table, use that one instead!
FROM (
SELECT goal player FROM actions UNION
SELECT assist FROM actions
) players
根据您的问题,我不确定 type = goal
是否与您的查询相关...
关于mysql - 按两列分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10055004/