两表连接--一题
2个表:
table1:
prd_no init_qty
A 19
B 10
C 3
TABLE2:
prd_no adj_qty
A 3
D 18
E 19
现在想要得到这样的查询结果:
prd_no init_qty adj_qty
A 19 3
B 10 0
C 3 0
D 0 18
E 0 19
查询语句应该怎么写?
sql方法:
-------------------------------
select temps.prd_no,IsNull(init_qty,0),IsNull(adj_qty,0) from (select prd_no from table1 union select prd_no from table2) as temps left join table1
on temps.prd_no = table1.prd_no left join table2 on temps.prd_no = table2.prd_no
----------------------------
select table1.prd_no,IsNull(init_qty,0) as init_qty,IsNull(adj_qty,0) as adj_qty from table1 left join table2 on table1.prd_no = table2.prd_no
union
select table2.prd_no,IsNull(init_qty,0) as init_qty,IsNull(adj_qty,0) as adj_qty from table2 left join table1 on table2.prd_no = table1.prd_no

浙公网安备 33010602011771号