当前位置: 代码迷 >> Sql Server >> 求一查询语句,解决办法
  详细解决方案

求一查询语句,解决办法

热度:99   发布时间:2016-04-27 17:05:29.0
求一查询语句,急
table1 table2
id numb conb id
1 2 2 1
2 3 4 1
  3 1
查询的结果为
id numb conb
1 2 2
  4
  3
请问如何写查询语句

------解决方案--------------------
SQL code
if object_id('tb1') is not null drop table tb1gocreate table tb1(id int,numb int)insert into tb1 values(1,2)insert into tb1 values(2,3)if object_id('tb2') is not null drop table tb2gocreate table tb2(id int,conb int)insert into tb2 values(1,2)insert into tb2 values(1,3)insert into tb2 values(1,4)select tb1.*,tb2.conb  from tb1 right join tb2 on tb2.id=tb1.id and tb1.numb =tb2.conbid          numb        conb----------- ----------- -----------1           2           2NULL        NULL        3NULL        NULL        4(3 行受影响)
------解决方案--------------------
SQL code
if object_id('tb1') is not null drop table tb1gocreate table tb1(id varchar(10),numb varchar(10))insert into tb1 values(1,2)insert into tb1 values(2,3)if object_id('tb2') is not null drop table tb2gocreate table tb2(id int,conb int)insert into tb2 values(1,2)insert into tb2 values(1,3)insert into tb2 values(1,4)select isnull(a.id,'') id,isnull(a.numb,'') numb,b.conb  from tb1 a right join tb2 b on a.id=b.id and a.numb =b.conbid         numb       conb---------- ---------- -----------1          2          2                      3                      4
------解决方案--------------------
探讨
ks_reny
查询的结果第二第三条里还是显示了1和2
而不是空
  相关解决方案