34,593
社区成员
发帖
与我相关
我的任务
分享
if not object_id('a') is null
drop table a
Go
Create table a([code] nvarchar(3),[name] nvarchar(3))
Insert a
select N'001',N'aaa' union all
select N'002',N'bbb' union all
select N'003',N'ccc' union all
select N'004',N'ddd'
Go
if not object_id('b') is null
drop table b
Go
Create table b([code] nvarchar(3),[info] int)
Insert b
select N'002',0
Go
select a.*,
isnull(b.info,1)info
from a left join b
on a.code=b.code
/*
code name info
---- ---- -----------
001 aaa 1
002 bbb 0
003 ccc 1
004 ddd 1
*/
select a.code,a.name,isnull(b.info,1)
from 表a a left join 表b b
on a.code=b.code
select a.*,
isnull(b.info,1)info
from a left join b
on a.code=b.code and a.name=b.name
select t1.code,t1.name,case when t2.info is null then 1 else t2.info end as info from a t1 left join b t2 on t1.code = t2.code
select a.code,a.name,b.info
from 表a a,表b b
where a.code=b.code