且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

mysql查询以查找在列中运行时间最长的

更新时间:2023-10-20 15:01:16

SELECT winner, MAX(winningStreak) FROM (
SELECT
winner,
IF(winner=@prev, @rownum:=@rownum+1, @rownum:=1) AS winningStreak,
@prev:=winner
FROM
yourTable
, (SELECT @prev:=NULL, @rownum:=1) vars
/*ORDER BY whateverDeterminesTheOrderOfTheWinners*/
)sq
GROUP BY winner
ORDER BY winningStreak DESC

您需要另一列来确定获奖者的顺序(就像您列出了获奖者一样)并调整查询中被注释掉的部分.除此之外,该查询还可以,但是用PHP确实会更容易.

You need another column which determines the order of the winners like you have listed them and adjust the outcommented part of the query. Apart from that this query works, but it would really be easier done in PHP.

此处实时查看.