[英]SQL select where id=1 for condition where id=2
假设我有一个这样的结果表:
| exam | score | user_id |
| 1 | 78 | 1 |
| 2 | 80 | 1 |
| 1 | 27 | 2 |
| 2 | 90 | 2 |
我想选择所有结果,其中,exam = 2,但同一用户在第一次考试中至少获得50分。
因此,例如这样的东西(我意识到这与SQL完全不同):
SELECT * from results r WHERE (for r.user WHERE r.exam = 2 FIND r.exam = 1 REQUIRE score > 50) AND r.exam = 2.
我将像您在问题中一样表达这一点:
select r.*
from results r
where r.exam = 2 and
exists (select 1
from results r2
where r2.userid = r.userid and
r2.exam = 1 and r2.score >= 50
);
首先,选择所有在第一次考试中得分大于或等于50的用户,然后基于user_id
加入表。
SELECT *
FROM Table
WHERE user_id IN(
SELECT T.user_id
FROM Table T
WHERE T.exam_id=1
AND T.score >= 50)
AND exam_id =2;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.