[英]Sql query find all rows where value is null but another row wih same value not null (Mariadb)
id_product|pn|ean13|supplier|
-----------------------------
1 |1F46G| FGH45642346|1|
2 |8BBBB| null |1|
3 |1F46G| null |2|
4 |1F46G| FGH45642346 |3|
您好,我有这样的表结构(只是更多行)。
我想要 select ean13 为 null 的所有行,但存在一些 PN 相同但 ean13 不是 null 的行。
SELECT id_product,pn
FROM product
WHERE pn !='' AND (ean13 is null OR ean13 = '')
GROUP BY pn
HAVING count(pn)>1
这 Select 部分工作但显示不存在的行下一行 ean13 不是 null
我试过使用 function exists 但持续时间真的很长。
基于您的精确描述的查询select 所有 ean13 为 null 但存在相同 PN 但 ean13 不是 null 的行的查询将如下所示。 没有任何迹象表明有任何聚合。
我不知道你的意思是我试过使用 function exists因为你没有包括这个。
select id_product, pn
from product p
where ean13 is null
and exists (
select * from product p2 where p2.pn = p.pn and p2.ean is not null
);
您可以如下重写查询以获得所需的 output。编写一个子查询以将具有 ean13 的 pn 标识为不是 null。
SELECT id_product,pn
from product
WHERE pn !='' AND (ean13 is null OR ean13 = '')
and pn in(
select pn from product where not (ean13 is null OR ean13 = '')
);
DB 小提琴: 结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.