日期:2014-05-16 浏览次数:20502 次
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2014-03-18 11:20:22
-- Verstion:
-- 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)
--
----------------------------------------------------------------
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go
create table [a]([COL1] int,[COL2] int)
insert [a]
select 1,1 union all
select 1,2 union all
select 1,3 union all
select 2,1 union all
select 2,2 union all
select 2,3
--> 测试数据:[b]
if object_id('[b]') is not null drop table [b]
go
create table [b]([COL1] int,[COL2] int)
insert [b]
select 1,1
--------------开始查询--------------------------
SELECT
MIN(a.col1) AS col1, b.COL2
FROM
(SELECT
*
FROM
a
WHERE
NOT EXISTS ( SELECT
1
FROM
b
WHERE
col1=a.col1 AND col2=a.col2 )) a
INNER JOIN b ON a.COL2=b.col2
GROUP BY
b.COL2
----------------结果----------------------------
/* col1 COL2
----------- -----------
2 1
*/