我正在尝试获取最后记录的亏损,然后计算此后的获胜次数,以计算出连胜纪录。
BEGIN
SET @maxDate:=(SELECT MAX(date) from game_scores where
user_score<opponent_score and user_id=1);
SELECT @maxDate as last_loss, COUNT(*) as streak from game_scores
WHERE user_score>opponent_score and date>@maxDate and user_id=1;
END
但是我在SET @maxDate行中不断收到语法错误。我靠近吗?
最佳答案
如果您加入最后一个损失日期的查询,则可以一次完成此操作。
另请注意,如果您已经找到了用户的上次丢失日期,则此后的任何记录都将获胜,因此您的第二次查询无需对照对手得分来检查用户得分。
SELECT
last_recorded_loss.maxDate AS last_loss,
COUNT(*) AS streak
FROM game_scores
INNER JOIN (
SELECT MAX(date) AS maxDate
FROM game_scores
WHERE user_score < opponent_score
AND user_id = 1
) last_recorded_loss
ON game_scores.date > last_recorded_loss.maxDate AND
game_scores.user_id = 1
关于mysql - MySQL对符合特定条件的行的max(date)之前的所有计数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30825149/