题目链接:点击打开链接
题目大意:略。
解题思路:略。
AC 代码
-- 解决方案(1) # db 临时表:列传行 with db as ( select * from ( select gold_medal as id,contest_id from contests union all select silver_medal as id,contest_id from contests union all select bronze_medal as id,contest_id from contests) as t order by id, contest_id) # 这里需要排序,不然变量遍历会出错 select name, mail from( #条件二:三场及更多不同的比赛中赢得金牌 select gold_medal as user_id from contests group by gold_medal having count(distinct contest_id) >= 3 union #条件一:该用户在连续三场及更多比赛中赢得奖牌 select distinct id as user_id from( select id, if(id = @lid, if(contest_id = @lcontest_id + 1, @cnt:=@cnt, @cnt:=@cnt+1), @cnt:=1) as cnt, @lid := id, @lcontest_id := contest_id from db, (select @lid:=-999, @lcontest_id:=-999, @cnt:=1) as u) as p group by id,cnt having count(cnt) >= 3) as y left join users m on y.user_id = m.user_id -- 解决方案(2) WITH t AS(SELECT gold_medal user_id, contest_id FROM Contests UNION ALL SELECT silver_medal, contest_id FROM Contests UNION ALL SELECT bronze_medal, contest_id FROM Contests), tt AS(SELECT t_1.user_id FROM t t_1 JOIN t t_2 ON t_1.user_id = t_2.user_id AND t_1.contest_id + 1 = t_2.contest_id JOIN t t_3 ON t_2.user_id = t_3.user_id AND t_2.contest_id + 1 = t_3.contest_id UNION SELECT gold_medal user_id FROM Contests GROUP BY gold_medal HAVING COUNT(*) > 2) SELECT name, mail FROM tt JOIN Users USING(user_id)