我有一个关系表,它有
employeeID | storeID
查询哪些员工在多家商店工作的查询是什么?
SELECT employeeID WHERE ???
并且每个员工也可能只列出一次不同的商店......
答案 0 :(得分:3)
使用group by
和having
,如:
select employeeID, count(*) from table group by employeeID having count(distinct storeID) > 1
这将为您提供在多个商店工作的员工。将其用作子查询,列出每个此类员工的商店。
答案 1 :(得分:1)
你可以尝试 -
select distinct employeeID,StoreID from table1
where storeID in
(
select storeID from table1 group by storeID having count(distinct employeeID) >1
)
存储计数并在一个查询中显示商店ID,您可以在查询后使用..
select a.employeeID,a.storeID,b.cnt
from table1 a,
(select employeeID,count(*) cnt
from table1
group by employeeID
having count(distinct storeID) >1) b
where a.employeID=b.employeeid