日期:2014-05-17 浏览次数:20693 次
--> 测试数据:#tb
IF OBJECT_ID('TEMPDB.DBO.#tb') IS NOT NULL DROP TABLE #tb
GO
CREATE TABLE #tb([ID] INT,[Num] INT)
INSERT #tb
SELECT 1,1 UNION ALL
SELECT 1,0 UNION ALL
SELECT 2,0 UNION ALL
SELECT 2,0 UNION ALL
SELECT 3,0 UNION ALL
SELECT 3,0
--------------开始查询--------------------------
SELECT * FROM #tb AS t WHERE NOT EXISTS(SELECT 1 FROM #tb WHERE [ID]=t.[ID] AND [Num]=1)
----------------结果----------------------------
/*
*
ID Num
2 0
2 0
3 0
3 0
*
*/
select *
from TB
where ID in (
select ID
from TB
where num=0
group by ID
having COUNT(1)>1)
--CREATE TABLE #tb (ID INT, Num INT )
--INSERT INTO #tb
--SELECT 1 ,1
--UNION ALL
--SELECT 1, 0
--UNION ALL
--SELECT 2, 0
--UNION ALL
--SELECT 2, 0
--UNION ALL
--SELECT 3, 0
--UNION ALL
--SELECT 3, 0
SELECT * FROM #tb
WHERE id IN (
SELECT id
FROM #tb
GROUP BY id
HAVING SUM(num)=0)
/*
ID Num
----------- -----------
2 0
2 0
3 0
3 0
*/