2014-11-19 3 views
0

두 개의 테이블이 있습니다. 표 "alldates"은 날짜 열 "nextdate"으로 구성됩니다. 표 "availability""availdate"이라는 날짜 열을 포함하여 여러 열로 구성됩니다. "alldates" 테이블 기본 키는 열 "nextdate"입니다. "availability" 테이블 기본 키는 "event_num"이라는 열입니다. 설명을 목적으로두 SQL 테이블의 데이터를 병합하는 방법은 무엇입니까?

는의 테이블 "alldates" 16 행으로 채워집니다 가정 해 봅시다 10/01/2014 through 10/16/2014 날짜 및 테이블 "availability"은과 같이, 9 행으로 채워집니다 :

Availdate  Event_num Event Description 
10/01/2014 3     Joe's birthday 
10/04/2014 12     Bill's dentist appt 
10/04/2014 5     Buy pizza 
10/05/2014 6     Clean the house 
10/07/2014 7     Go to theater 
10/07/2014 8     Continue forward 
10/09/2014 9     Mow the grass 
10/11/2014 10     Take a nap 
10/15/2014 11     Fix the clock 

내가 다음과 같습니다 새 테이블을 작성해야합니다 :

Availdate  Event_num Event Description 
10/01/2014 3     Joe's birthday 
10/02/2014 (from table "alldates") 
10/03/2014 (from table "alldates") 
10/04/2014 12     Bill's dentist appt 
10/04/2014 5     Buy pizza 
10/05/2014 6     Clean the house 
10/06/2014 (from table "alldates") 
10/07/2014 7     Go to theater 
10/07/2014 8     Continue forward 
10/08/2014 (from table "alldates") 
10/09/2014 9     Mow the grass 
10/10/2014 (from table "alldates") 
10/11/2014 10     Take a nap 
10/12/2014 (from table "alldates") 
10/13/2014 (from table "alldates") 
10/14/2014 (from table "alldates") 
10/15/2014 11     Fix the clock 
10/16/2014 (from table "alldates") 

답변

0

테이블 모두 열 수가 동일한 경우에 당신은 아마 그들 사이 UNIONorder byAvaildate 콜 럼을 수행 할 수 있습니다 n

select * from 
(
select Availdate, 
Event_num, 
Event Description 
from alldates 

UNION 

select Availdate, 
Event_num, 
Event Description 
from availability 
) tab  
order by Availdate 
0

원하는 내용은 LEFT OUTER JOIN입니다. LEFT OUTER은 두 번째 테이블에 해당 항목이 없더라도 첫 번째 테이블의 행을 포함하도록 SQL에 알립니다. "대응"은 술어에 의해 결정됩니다.이 조건은 한 테이블의 행과 다른 테이블의 행이 어; 게 연관되는지를 데이터베이스에 알려주는 조건입니다.

아래 쿼리는 alldates에서 모든 행을 선택합니다. nextdate이 행의 availdate에 일치하는 사람이 availability 인 경우 각 행은 일치하는 행당 한 번 표시됩니다. nextdateavailability과 일치하지 않는 경우 event_num 및 event_description은 NULL으로 표시됩니다.

SELECT 
    alldates.nextdate, 
    availability.event_num, 
    availability.event_description 
FROM 
    ALLDATES 
    LEFT JOIN availability 
ON alldates.nextdate = availability.availdate; 
1

나에게 고전적인 왼쪽 조인처럼 보입니다.

select a.nextdate as availdate, 
b.event_num, 
b.event_description 
from alldates a 
left join availability b 
on a.nextdate = b.availdate 
; 
관련 문제