日期:2014-05-17 浏览次数:20467 次
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-12-23 16:09:31
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[A]
if object_id('[A]') is not null drop table [A]
go
create table [A]([A01] varchar(3))
insert [A]
select 'm01' union all
select 'm02'
--> 测试数据:[B]
if object_id('[B]') is not null drop table [B]
go
create table [B]([B01] varchar(3),[B02] varchar(2))
insert [B]
select 'm01','ok' union all
select 'm01','ok' union all
select 'm01','no' union all
select 'm02','ok' union ALL
select 'm02','ok'
--------------开始查询--------------------------
select * from [A]
WHERE EXISTS (SELECT 1 FROM b WHERE a.[a01]=b.[b01] AND b.[b02]='ok')
AND NOT EXISTS (SELECT 1 FROM b WHERE a.[a01]=b.[b01] AND b.[b02]='no')
----------------结果----------------------------
/*
A01
----
m02
*/