我不确定这个查询

问题描述 投票:0回答:2

当且仅当客户表中的一个或多个客户位于伦敦时,查询才从客户表中提取数据

我的查询是

select * from Customer
where 'London' = ANY (select city from Customer)

我知道这不是正确的方法,但我想知道这是否也有效。 TIA

sql
2个回答
2
投票

您的查询有效,因此这是表达它的一种方式(而且非常聪明)。 Here就是一个例子。

更常见的是,我认为这是anexists查询:

select c.*
from Customer c
where exists (select 1 from Customer c2 where c2.city = 'London');

这将返回任何城市的所有客户,即使是那些不在伦敦的客户。

您很可能只想回到伦敦的客户,在这种情况下,简单的where city = 'London'就足够了。


0
投票
select * from Customer where city ='London' and 1 > (select count(*) from Customer where city='London');
© www.soinside.com 2019 - 2024. All rights reserved.