我试图提取单独加载数字1或8的名称,但查询不这样做。
所以想看看名字只有8个或只有1个而不是两个
CREATE TABLE Table1
(`name` varchar(7), `number` int);
INSERT INTO Table1
(`name`, `number`)
VALUES
('renee', 1),
('renee', 8),
('eugene', 1),
('jacques', 1),
('jacques', 8),
('avril', 8);
SELECT Name FROM Table1 WHERE Number IN ('1')
AND Number NOT IN ('8')
答案 0 :(得分:4)
解决此问题的最简单方法可能是按名称汇总,限制为1或8个数字值,然后仅保留具有一个不同数字的名称。
SELECT Name
FROM Table1
WHERE Number IN (1, 8)
GROUP BY Name
HAVING COUNT(DISTINCT Number) = 1
答案 1 :(得分:1)
以下查询完全按照要求进行操作,例如,如果记录(名称,编号)不唯一=1
可能会更改为>=1
,则可能无效。
SELECT Name FROM Table1 WHERE Number in (1,8)
group by name
having count(case when number=1 then 1 end)=1
and count(case when number=8 then 1 end)=0
or count(case when number=1 then 1 end)=0
and count(case when number=8 then 1 end)=1