2 열 customer 및 city가있는 테이블이 있습니다. 4 개의 고유 한 도시 (w, x, y, z)가 있습니다. 모든 도시와 관련된 고객을 원합니다. 그래서 테이블
+ ---------- + ------ + | 고객 | 도시 | + ---------- + ------ + | A | W | | A | X | | A | Y | | A | Z | | B | X | | C | Y | | Z | X | | Z | Y | + ---------- + ------ +
원하는 출력은
+ ---------- + ------ + | 고객 | 도시 | + ---------- + ------ + | A | W | | A | X | | A | Y | | A | Z | + ---------- + ------ +
내가 사용한 쿼리는 다음과 같습니다.
Select Customer,City
From Table As T
Where Exists (
Select 1
From Table As T2
Where T2.Customer = T.City
And T2.City<> T.City
)
HAVING 절의 집계 및 조건을 사용하면 원하는 고객을 얻을 수 있습니다.
select customer
from tablename
group by customer
having count(*) = (select count(distinct city) from tablename)
연산자 IN과 함께 사용할 수 있습니다.
select * from tablename
where customer in (
select customer
from tablename
group by customer
having count(*) = (select count(distinct city) from tablename)
)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다