gpt4 book ai didi

Mysql 查询 - 无法找到执行此操作的方法

转载 作者:行者123 更新时间:2023-11-29 02:03:06 26 4
gpt4 key购买 nike

INSERT INTO `table` (`game_id`, `first`, `second`, `third`)
VALUES
(1, 'jack', 'joe', 'pat'),
(2, 'jack', 'joe', 'jess'),
(3, 'pat', 'jess', 'jack'),
(4, 'pat', 'jess', 'jack');

这是一个统计表,其中包含每场比赛的前三名球员。我希望拉动所有玩家并相应地对其进行排序。

First place - 3 points
Second place - 2 points
Third place - 1 point

所以,它应该返回:

id  player  points
1 jack 8
2 pat 7
3 jess 5
4 joe 4

我想不出一种方法来用一个查询来做到这一点。

最佳答案

Select player, sum(points) as points from (
Select `first` as player, count(`first`)*3 as points From gameStats group by `first`
union all
Select `second` as player, count(`second`)*2 as points From gameStats group by `second`
union all
Select `third` as player, count(`third`)*1 as points From gameStats group by `third` ) as tmp group by player

应该这样做。

如果您需要更多帮助,请告诉我。

关于Mysql 查询 - 无法找到执行此操作的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11475600/

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