0

我对 SQL 很陌生,我遇到了这个问题:我有关系表,其中人有权力

A has X
A has Z
B has X
B has Y
C has Y
D has Z

我想选择所有具有 X 和 Y 权力的人,在这种情况下是 B。当然,我会有一个更复杂的权力组合。

4

2 回答 2

1

做这样的事情,

SELECT person
FROM tableName
WHERE power IN ('x', 'y')
GROUP BY Person
HAVING COUNT(*) = 2

如果没有对每个人强制执行唯一约束power,则需要使用DISTINCT

SELECT person
FROM tableName
WHERE power IN ('x', 'y')
GROUP BY Person
HAVING COUNT(DISTINCT power) = 2
于 2012-12-22T13:54:37.140 回答
1

这称为关系除法,这是一种方法:

SELECT person
FROM Powers
WHERE power IN ('x', 'y')
GROUP BY person
HAVING COUNT(power) = 2
于 2012-12-22T13:54:38.023 回答