我试图仅从那些在 Pno 3但不在10
中工作的人那里获得下表中的 Essn 值所以结果应该是"666884444"
我试着像
那样做SELECT Essn
FROM WORKS_ON
WHERE Pno = 3 and Pno != 10;
但是无法获得正确的结果
WORKS_ON
---Essn-----------Pno-----------Hours----
"123456789" "1" "32.5"
"123456789" "2" "7.5"
"666884444" "3" "40"
"453453453" "1" "20"
"453453453" "2" "20"
"333445555" "2" "10"
"333445555" "3" "10"
"333445555" "10" "10"
"333445555" "20" "10"
"999887777" "30" "30"
"999887777" "10" "10"
"987987987" "10" "35"
"987987987" "30" "5"
"987654321" "30" "20"
"987654321" "20" "15"
"888665555" "20" NULL
答案 0 :(得分:3)
你的WHERE-Condition不起作用,因为它只是分别比较每一行的值。 有很多方法可以实现这一点,这是我的第一个想法:
SELECT DISTINCT Essn
FROM WORKS_ON
WHERE Pno = 3 AND Essn NOT IN (SELECT Essn FROM WORKS_ON WHERE Pno = 10);
答案 1 :(得分:0)
我认为您可以使用group by
和having
执行您想要的操作:
SELECT Essn
FROM WORKS_ON
GROUP BY Essn
HAVING SUM(CASE WHEN Pno = 3 THEN 1 ELSE 0 END) > 0 AND -- has 3
SUM(CASE WHEN Pno = 10 THEN 1 ELSE 0 END) = 0; -- does not have 10
答案 2 :(得分:0)
SELECT Essn
FROM WORKS_ON
WHERE Pno = "3"
这本身只会返回Essn
列的Pno
,其值为3.
为什么还需要另一个WHERE Pno != 10
。