下面是我的查询,它返回特定用户=27的条纹:
SELECT MAX(sum) AS streak
FROM (
SELECT
game_date,
IF(points > 0, @sum:=@sum+1, @sum:=0) AS sum
FROM (
SELECT
game_date,
(SELECT COUNT(*) FROM point WHERE user_id = 27 AND bet_id = b.id AND goals > 0) AS points
FROM bet b
WHERE game_date < NOW()
ORDER BY game_date DESC
) t1, (SELECT @sum:=0) t2
) t;
**我的尝试:**它在我的本地MySQL上运行成功,但在实时phpMyAdmin上给出一个错误,即user_id是where子句中的未知列。
SELECT DISTINCT user_id,(
SELECT MAX(SUM) AS streak
FROM (
SELECT
game_date,
IF(points > 0, @sum:=@sum+1, @sum:=0) AS SUM
FROM (
SELECT
game_date,
(SELECT COUNT(*) FROM POINT WHERE user_id = p.user_id AND bet_id = b.id AND goals > 0) AS points
FROM bet b
WHERE game_date < NOW()
ORDER BY game_date DESC
) t1, (SELECT @sum:=0) t2
) t) AS streak FROM POINT p;
1条答案
按热度按时间ut6juiuv1#
如果没有任何样本数据来测试,我可能会偏离主题,但给予这个:
这是gaps-and-islands问题的一个变体,该查询采用了一种不同的(“现代”)方法,使用ROW_NUMBER()window function-