日期:2014-05-17 浏览次数:20546 次
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-10-21 13:06:01
-- 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: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([imgtag_id] int,[mymaterial_id] int)
insert [huang]
select 1,23 union all
select 1,24 union all
select 1,25 union all
select 2,23 union all
select 2,24
--------------开始查询--------------------------
select [mymaterial_id] from [huang]a
WHERE imgtag_id=1
INTERSECT
select [mymaterial_id] from [huang]a
WHERE imgtag_id=2
----------------结果----------------------------
/*
mymaterial_id
-------------
23
24
*/
create table tab([imgtag_id] int,[mymaterial_id] int)
insert tab
select 1,23 union all
select 1,24 union all
select 1,25 union all
select 2,23 union all
select 2,24
select mymaterial_id
from tab
where ImgTag_Id in (1,2)
group by mymaterial_id
having COUNT(*)>1
/*
mymaterial_id
23
24
*/