2016-06-03 4 views
0

두 테이블에서 고유 한 이메일 주소 목록을 가져와야합니다. 예를 들어 내가 선택했습니다 :2 개의 테이블에서 고유 한 정렬 된 열 값 목록

select distinct 
    email 
from 
    contacts 
order by 
    email 

select distinct 
    email 
from 
    customers 
order by 
    email 

그 중 하나만 필요한 경우, 케이크 조각. 내가 2 열로 나란히 놓기를 원한다면, 케이크 조각도.

하지만 어떻게 이들을 하나의 열 (중복되지 않음)로 정렬합니까? 이것이 유용하다면 이것은 Azure Sql Database에서 실행될 것입니다. 같은 대해 어떻게

답변

0

:

select distinct email from contacts order by email // Your first query 
union 
select distinct email from customers order by email // Your second query 

union 이미 두 쿼리의 데이터를 하나의 열 이메일을 작성하고이 union all 달리 중복을 제거합니다.

주문하려면 ORDER BY email을 끝에 추가하십시오.

0
select distinct 
    email 
from (
    select distinct email from contacts order by email 
    union all 
    select distinct email from customers order by email 
    ) as emails 
관련 문제