日期:2014-05-18  浏览次数:20482 次

求一查询语句,急
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 tb1
go
create 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 tb2
go
create 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.conb

id          numb        conb
----------- ----------- -----------
1           2           2
NULL        NULL        3
NULL        NULL        4

(3 行受影响)

------解决方案--------------------
SQL code
if object_id('tb1') is not null drop table tb1
go
create 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 tb2
go
create 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.conb

id         numb       conb
---------- ---------- -----------
1          2          2
                      3
                      4

------解决方案--------------------
探讨
ks_reny
查询的结果第二第三条里还是显示了1和2
而不是空