34,590
社区成员
发帖
与我相关
我的任务
分享
--设两表名为t1,t2
select t1.a,t1.b,isnull(t2.c,0) as C
from T1 left join T2 on t1.a=t2.a
if object_id('tempdb.dbo.#TB') is not null drop table #TB
go
create table #TB([A] int,[B] int)
insert #TB
select 1,1 union all
select 2,1 union all
select 3,1
--> 测试数据:#TB1
if object_id('tempdb.dbo.#TB1') is not null drop table #TB1
go
create table #TB1([A] int,[C] int)
insert #TB1
select 1,1 union all
select 4,1
select A.*,isnull(B.[C],0) C from #TB A left join #TB1 B on A.[A]=B.[A]
A B C
----------- ----------- -----------
1 1 1
2 1 0
3 1 0
----------------------------------------------------------------
-- Author :SQL77(只为思齐老)
-- Date :2010-01-20 13:29:19
-- Version:
-- Microsoft SQL Server 2000 - 8.00.194 (Intel X86)
-- Aug 6 2000 00:57:48
-- Copyright (c) 1988-2000 Microsoft Corporation
-- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:#TB
if object_id('tempdb.dbo.#TB') is not null drop table #TB
go
create table #TB([A] int,[B] int)
insert #TB
select 1,1 union all
select 2,1 union all
select 3,1
--> 测试数据:#TB1
if object_id('tempdb.dbo.#TB1') is not null drop table #TB1
go
create table #TB1([A] int,[C] int)
insert #TB1
select 1,1 union all
select 4,1
--------------开始查询--------------------------
select A.A,A.B,ISNULL(B.C,0)C from #TB A LEFT JOIN #TB1 B ON A.A=B.A
----------------结果----------------------------
/*
(所影响的行数为 3 行)
(所影响的行数为 2 行)
A B C
----------- ----------- -----------
1 1 1
2 1 0
3 1 0
(所影响的行数为 3 行)
*/
select
a.*,isnull(b.c,0) as c
from
a left join b
on
a.a=b.a
select a.*,isnull(b.c,0) as c
from ta a
left join tb b
on a.a=b.a