1

我有一张表,其结构是:

+----+-------+----------+
| ID | Class | Names    |
+----+-------+----------+
| 1  | Paper |    Ed    |
+----+-------+----------+
| 2  | Glass |    Pete  |
+----+-------+----------+
| 3  | Glass |    Ed    |
+----+-------+----------+
| 4  | Rock  |    Fred  |
+----+-------+----------+

我想找到有一类纸和玻璃的名字。所以在这个小表中它会返回:

+----+-------+----------+
| ID | Class | Names    |
+----+-------+----------+
| 1  | Paper |    Ed    |
+----+-------+----------+
| 3  | Glass |    Ed    |
+----+-------+----------+

但是,名称不能是查询的一部分。必须找到 class = paper 和 class = glass 的名称。所以我只想要有纸和玻璃的名字。

简化的 SQL 将是:

SELECT NamesFROM table WHERE Class= Paper AND Class= Glass

显然,这不起作用,因为一行只有一个类值。

TIA

4

1 回答 1

1
SELECT Names 
FROM table 
WHERE Class IN ('Paper','Glass') -- list of Class
GROUP BY Names
HAVING COUNT(*) = 2              -- number of class

Class在每个中都不是唯一的情况下names, aDISTINCT只需要计算唯一值。

HAVING COUNT(DISTINCT Class) = 2 
于 2013-10-15T09:13:21.100 回答