我正试图获得最后一次记录的损失,然后计算之后的胜利次数来计算连胜。
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行得到语法错误。我接近了吗?
答案 0 :(得分:2)
如果您加入上次丢失日期的查询,则可以一次性执行此操作。
另请注意,如果您已经找到了用户的上次丢失日期,那么他们之后的任何记录都将获胜,因此您的第二个查询无需根据对手得分检查用户得分。
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