我使用 SQL(SQL Server、PostgreSQL)已有 10 多年了,但我仍然从未在我的生产代码中使用过关键字ANY/SOME
。ALL
我遇到的所有情况都可以使用IN
, MAX
, MIN
, EXISTS
, 并且我认为它更具可读性。
例如:
-- = ANY
select * from Users as U where U.ID = ANY(select P.User_ID from Payments as P);
-- IN
select * from Users as U where U.ID IN (select P.User_ID from Payments as P);
或者
-- < ANY
select * from Users as U where U.Salary < ANY(select P.Amount from Payments as P);
-- EXISTS
select * from Users as U where EXISTS (select * from Payments as P where P.Amount > U.Salary);
使用ANY/SOME
和ALL
:
所以问题是:我错过了什么吗?是否存在某些情况ANY/SOME
并ALL
超越其他解决方案?