次の結合を取得する方法
入力:
table1: table2:
col1 Col1 col2 col3
A A 1 2
B B 4 5
C
出力:
col1 col2 col3
A 1 2
B 4 5
c - -
select t1.col1, t2.col2, t2.col3
from table1 t1
left outer join table2 t2
on t1.col1 = t2.col1
SELECT t1.col1, t2.col2, t2.col3
FROM table1 t1
LEFT JOIN table2 t2 ON t1.col1=t2.col1
;
外部結合を行う必要があります
SELECT *
FROM table1 t1,
table2 t2
WHERE t1.col1 = t2.col1(+)
「-」を付けたくない場合は、このようなものかもしれません
SELECT t1.col1, coalesce(t2.col2,'-') as col2, coalesce(t2.col3,'-') as col3
FROM table1 t1
LEFT JOIN table2 t2 ON t1.col1=t2.col1
私のsqlfiddleを参照してください
create table table1(
col1 char(1)
);
insert into table1 values('A');
insert into table1 values('B');
insert into table1 values('C');
create table table2(
col1 char(1),
col2 int,
col3 int
);
insert into table2 values('A',1,2);
insert into table2 values('B',4,5);
select
t1.col1,
coalesce(t2.col2,'-'),
coalesce(t2.col3,'-')
from
table1 t1
left join table2 t2 on t1.col1=t2.col1
;