我从sql table(T-SQL)获取的数据如下:
ID | Name | Status | Date
1 | Tom | 1 |20140101
2 | Tom | 2 |20140301
3 | Dan | 1 |20140301
4 | Tom | 3 |20140302
5 | Dan | 3 |20140304
6 | Sam | 2 |20140405
7 | Tom | 4 |20140406
需要选择状态为3的所有用户,但如果用户获得状态4或更高,则不应显示。
在此示例中,只显示ID为5的Dan。
答案 0 :(得分:2)
试试这个:
SELECT * FROM T
WHERE NAME NOT IN (SELECT NAME FROM T WHERE STATUS >= 4)
AND STATUS = 3
答案 1 :(得分:1)
SELECT *
FROM USERS U1
WHERE
Status = 3
AND NOT EXISTS(
SELECT 1 FROM
USERS U2
WHERE
Status > 3
AND U1.Name = U2.Name
)
答案 2 :(得分:0)
SELECT * FROM `table` WHERE status = 3
只会返回状态等于3
的用户答案 3 :(得分:0)
您只需使用NOT EXISTS
:
select * from t t1
where t1.status = 3 and not exists
(select 1 from t t2 where status > 3 and t1.Name = t2.Name)