首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求一查询语句,解决办法

2012-01-29 
求一查询语句,急table1table2id numbconb id1221234131查询的结果为id numb conb12243请问如何写查询语句[

求一查询语句,急
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
而不是空

热点排行