日期:2014-05-16 浏览次数:20750 次
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2014-03-28 16:46:56
-- Version:
-- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[table1]
if object_id('[table1]') is not null drop table [table1]
go
create table [table1]([tid1] int,[name] varchar(4))
insert [table1]
select 1,'张一' union all
select 2,'张二' union all
select 3,'张三' union all
select 4,'张四' union all
select 5,'张五'
--> 测试数据:[table2]
if object_id('[table2]') is not null drop table [table2]
go
create table [table2]([tid2] int,[tid1] int,[time] datetime)
insert [table2]
select 1,1,'2013-01-12' union all
select 2,1,'2013-03-12' union all
select 3,1,'2013-02-15' union all
select 4,3,'2013-05-12' union all
select 5,2,'2014-03-10' union all
select 6,2,'2014-10-10'
--------------开始查询--------------------------
SELECT
a.tid1,a.name,b.time
FROM
[table1] AS a
LEFT JOIN [table2] AS b ON a.tid1=b.tid1
WHERE
NOT EXISTS ( SELECT
1
FROM
&nbs