我有一张表如下
id price
1 100
2 103
3 101
4 102
5 106
6 107
Run Code Online (Sandbox Code Playgroud)
'''
我想写一个 sql 命令来选择那些高于所有以前记录的记录。
1 100
2 103
5 106
6 107
Run Code Online (Sandbox Code Playgroud)
谢谢
您可以将 NOT EXISTS 条件与相关子查询一起使用:
select *
from the_table d1
where not exists (select *
from the_table d2
where d2.id < d1.id
and d2.price > d1.price);
Run Code Online (Sandbox Code Playgroud)
这将返回不存在具有较小id但较大的行的所有行price
另一种解决方案是使用ALL运算符:
select *
from the_table d1
where price > all (select price
from the_table d2
where d2.id < d1.id);
Run Code Online (Sandbox Code Playgroud)