检索表中没有出现的每条记录

时间:2014-09-15 07:09:12

标签: sql-server-2008

我的表格在这里给出了......

Appear   Back_Color  Fore_Color

simple    White         Black
simple    white         Black
medium    Gray          Black
medium    Gray          Black

现在我想检索所有没有出现的记录..

即结果应该是这样的......

Appear   Back_Color  Fore_Color   count

simple    White         Black      2
medium    Gray          Black      2

这里可以使用哪些代码?

谢谢和问候,  Nijisha K C

1 个答案:

答案 0 :(得分:0)

也许是这样的:

SELECT Appear, Back_Color, Fore_Color, COUNT(*) AS count
FROM Table1 
GROUP BY Appear, Back_Color, Fore_Color

<强>更新

考虑您提供的测试数据:

DECLARE @tbl TABLE(Appear VARCHAR(10),Back_Color VARCHAR(10),
                   Fore_Color VARCHAR(10))

INSERT INTO @tbl
VALUES
('simple','White','Black'),
('simple','white','Black'),
('medium','Gray','Black'),
('medium','Gray','Black')

使用此查询:

SELECT Appear, Back_Color, Fore_Color, COUNT(*) AS count
FROM @tbl 
GROUP BY Appear, Back_Color, Fore_Color

输出如下:

Appear  Back_Color Fore_Color count
medium  Gray       Black      2
simple  White      Black      2

就像你期望的那样