gpt4 book ai didi

php - mySQL 计数内容和自动 +1

转载 作者:行者123 更新时间:2023-11-30 23:50:58 25 4
gpt4 key购买 nike

我正在尝试对来自此页面的数据进行排序:http://www.excelwrestling.com/sortbymostwinsbyweight.php

我希望表格按重量排序,然后在该重量下获胜最多的名字。另外,我想再添加一列,显示该摔跤手的获胜次数。

这是我在查看教程时得到的结果,但它们的排序不正确:

$query="SELECT *,Winner, COUNT(*) as count FROM results GROUP BY Winner ORDER BY Weight ASC ";
$result=mysql_query($query);

谢谢!

最佳答案

SELECT ww.*
FROM
( SELECT Weight, Winner, COUNT(*) AS Wins
FROM results
GROUP BY Weight, Winner
) AS ww
JOIN
( SELECT DISTINCT Weight
FROM results
) AS dw
ON ww.Weight = dw.Weight
AND ww.Winner =
( SELECT r.Winner
FROM results AS r
WHERE r.Weight = dw.Weight
GROUP BY r.Winner
ORDER BY COUNT(*) DESC
LIMIT 1
) ;

(Weight, Winner) 上的索引将有助于提高查询效率。


你也可以通过这个查询得到正确的结果,但我不推荐它,因为它使用了 MySQL 的一些非标准特性和行为,因此将来可能会中断:

SELECT Weight, Winner, Wins
FROM
( SELECT Weight, Winner, COUNT(*) AS Wins
FROM results
GROUP BY Weight, Winner
ORDER BY Weight, Wins DESC
) AS ww
GROUP BY Weight ;

关于php - mySQL 计数内容和自动 +1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11286010/

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