gpt4 book ai didi

php - MySQL - 我如何/解决方法按组函数的结果排序

转载 作者:行者123 更新时间:2023-11-29 06:36:39 25 4
gpt4 key购买 nike

我正在使用 MySQL 5.5.37-0 ubuntu0.12.04.1

我有以下用于足球比赛的 MySQL 表..

G = 目标
M = 错过了
S = 已保存

id|  team  | result

0, "England", "G",
1, "France", "M",
2, "England", "G",
3, "Germany", "G",
4, "Germany", "S",

等...

我需要创建最佳球队的联赛表,按他们的失球率排序......

我想做以下...


SELECT `team` as 'target',
(
SELECT count(`id`)
FROM `database`
WHERE `team` = `target`
) as 'shots',
count(`id`) AS 'goals'
FROM `database`
WHERE (
`result` = 'G'
)
GROUP BY `team`
ORDER BY (`goals` / `shots`) DESC
Limit 0, 10
</code>

但您不能订购群组功能。有没有其他方法可以做到这一点?

谢谢。

最佳答案

SELECT COUNT(result)  shots,
(SELECT COUNT(result) AS Goal FROM football
WHERE result='g' AND team = f.team
) goal
,team , ((SELECT COUNT(result) AS Goal FROM football
WHERE result='g' AND team = f.team))/COUNT(result) AS ratio

FROM football f
GROUP BY team


ORDER BY ratio DESC

请尝试这个查询,让我知道我创建了你的表,正如你在问题中提到的那样,并尝试找到罢工率和目标完成率....

关于php - MySQL - 我如何/解决方法按组函数的结果排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24203847/

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