我在这张桌子上查询:
SKU
aaaa
bbbb
bbbb
NULL
以下是查询:
select *
from TEST as N
where N.SKU NOT IN (select SKU
from TEST
group by SKU
having count(*)>1);
我希望查询返回“aaaa”,但是它不会返回任何内容。
我期望的原因是因为下面的子查询只返回'bbbb':
select SKU
from TEST
group by SKU
having count(*)>1
因此,'aaaa'不在子查询结果中。
要显示错误,请将这些语句复制并粘贴到MySQL IDE中以创建架构:
drop table if exists TEST;
create table TEST(
SKU varchar(255)
);
insert into TEST values('aaaa'),('bbbb'),('bbbb'),(NULL);
答案 0 :(得分:0)
如果子查询只返回一行,这可以正常工作:
SELECT *
FROM TEST as N
WHERE N.SKU NOT IN (
CAST((select SKU from TEST group by SKU having count(*)>1) AS CHAR)
)
但是,如果它返回的行数超过1行,则会触发错误。另一个解决方案(如果子查询返回超过1行):
SELECT *
FROM TEST as N
WHERE N.SKU IN (
(select SKU from TEST group by SKU having count(*) <= 1)
)