使用set操作的SQL查询?问题:查找在同一城市处理过 "房子 "和 "公寓 "类型物业的所有员工的编号。

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

我使用的是Dreamhome数据库。这是我的查询。

select (staffno) from propertyforrent where staffno in(select staffno from propertyforrent where type ='House'unionselect staffno from propertyforrent where type ='Flat')and city= 'Glasgow')enter image description here

我已经试过了。但它是错误的。

sql union
1个回答
1
投票

使用聚合。

select distinct staffno
from t
where type in ('House', 'Flat')
group by staffno, city
having count(distinct type) = 2;

这是极少数的情况之一 select distinct 是与 group by. 你并没有要求城市,所以这只是返回员工编号,即使有多个城市。

© www.soinside.com 2019 - 2024. All rights reserved.