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

sql server 2005中查询记录为空,怎么让此行照样显示

2012-05-30 
sql server 2005中查询记录为空,如何让此行照样显示?首先 ,加入有两张表tb1 列名id0nametb2列名id1otherid

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 allselect 1,'wang'declare @tb2 table(id1 int ,other varchar(20))insert into @tb2select 0,'beijing'select a.*,b.other from @tb1 as a left join @tb2 b on a.id0=b.id1/*id0         name                 other----------- -------------------- --------------------0           liu                  beijing1           wang                 NULL(2 行受影响)*/ 

热点排行