日期:2014-05-16 浏览次数:20543 次
----------------------------------------------------------------
-- Author :DBA_HuangZJ(发粪涂墙)
-- Date :2014-05-06 14:43:49
-- Version:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64)
-- Apr 2 2010 15:48:46
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据[teacher]
if object_id('[teacher]') is not null drop table [teacher]
go
create table [teacher]([teacherID] int,[Name] nvarchar(4),[class] nvarchar(8))
insert [teacher]
select 1,N'张三',N'初一二班' union all
select 2,N'李四',N'初一三班' union all
select 3,N'张三',N'初一一班' union all
select 4,N'王五',N'初一二班' union all
select 5,N'刘六',N'初一二班' union all
select 6,N'王五',N'初二一班'
--------------生成数据--------------------------
SELECT * FROM TEACHER
WHERE NAME IN (
select name
from teacher
group by name
having count(class)=2)
----------------结果----------------------------
/*
teacherID Name class
----------- ---- --------
1 张三 初一二班
3 张三 初一一班
4 王五 初一二班
6 &n