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

sql server 2005中查询记录为空,如何让此行照样显示?
首先 ,加入有两张表

tb1 列名  

  id0
  name

tb2 列名
  id1
  other

id0 和id1是主键,tb2 add constraint fk (id1) refer... tb1(id0)

插入数据(就简写了): insert tb1 (0,liu)
  (1,wang)
  insert tb2(0,beijing)
   

查询语句:select tb1.id0,tb1.name,tb2.other from id0 where id0=id1

  结果只能显示 0,liu,beijing;

我的目的是 显示出 0,liu,beijing
  1,wang,null(或无)

用什么方法?
 

------解决方案--------------------
SQL code

declare @tb1 table (id0 int,name varchar(20))
insert into @tb1 
select 0,'liu' union all
select 1,'wang'
declare @tb2 table(id1 int ,other varchar(20))
insert into @tb2
select 0,'beijing'
select a.*,b.other from @tb1 as a left join @tb2 b on a.id0=b.id1
/*
id0         name                 other
----------- -------------------- --------------------
0           liu                  beijing
1           wang                 NULL

(2 行受影响)
*/