日期:2014-05-16 浏览次数:20679 次
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2014-03-24 14:54:11
-- 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)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([col1] varchar(4),[col2] varchar(1))
insert [tb]
select 'a001','h' union all
select 'a002','v' union all
select 'a003','h' union all
select 'a001','h' union all
select 'a002','v' union all
select 'a003','w' union all
select 'a001','m' union all
select 'a002','v' union all
select 'a003','h'
--------------开始查询--------------------------
SELECT DISTINCT * FROM tb t WHERE EXISTS(SELECT 1 FROM tb WHERE col1=t.col1 AND col2<>t.col2)
----------------结果----------------------------
/* col1 col2
---- ----
a001 h
a001 m
a003 h
a003 w
(4 行受影响)
*/