当前位置: 代码迷 >> Sql Server >> 请问一个关于组合查询的有关问题
  详细解决方案

请问一个关于组合查询的有关问题

热度:88   发布时间:2016-04-27 17:50:27.0
请教一个关于组合查询的问题!
table   a
col1   col2   col3   col4
1           1         1         1
2           2         2         2
3           3         3         3

table   b

col1   col2   col3   col4   col5
1           1         1         1         a1
d           c         f       e           g
2           2         2         2         a2
3           3         3         3         a3

如何得到
col1   col2   col3   col4   col5
1           1         1         1       a1
2           2         2         2       a2
3           3         3         3       a3
table   b中第二行为不规则数据,需剔除
table   a中为提出不规则数据后的表,但没有col5字段

------解决方案--------------------
用 left join 。


select A.*,B.col5
from A
left join B on A.col1 =B.col1 and A.col2=B.col2 and A.col3=B.col3 and A.col4=B.col4
------解决方案--------------------
select A.*,B.col5
from A left join B on A.col1 =B.col1 and A.col2=B.col2 and A.col3=B.col3 and A.col4=B.col4会出现A有B没有的值

简单的可以用
select A.*,B.col5
from A,B where A.col1 =B.col1 and A.col2=B.col2 and A.col3=B.col3 and A.col4=B.col4

不过表大效率不高,还是用inner join
  相关解决方案