读书人

sql server 2005中查询记录为空怎么

发布时间: 2012-05-30 20:20:04 作者: rapoo

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 行受影响)*/ 

读书人网 >SQL Server

热点推荐