gpt4 book ai didi

mysql - 按两列分组

转载 作者:行者123 更新时间:2023-11-29 01:44:41 25 4
gpt4 key购买 nike

数据库表包含目标操作。

 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com