[英]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|
Hello I have table structure like this (just more rows).您好,我有这样的表结构(只是更多行)。
I want select all rows where ean13 is null but exists some row where is same PN but ean13 is not null.我想要 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
This Select partly working but shows rows where not exists next row with ean13 which is not null这 Select 部分工作但显示不存在的行下一行 ean13 不是 null
I tried use function exists but the duration is really long time.我试过使用 function exists 但持续时间真的很长。
A query based on your precise description select all rows where ean13 is null but exists some row where is same PN but ean13 is not null would look like the following.基于您的精确描述的查询select 所有 ean13 为 null 但存在相同 PN 但 ean13 不是 null 的行的查询将如下所示。 There's nothing that would indicate any aggregation.没有任何迹象表明有任何聚合。
I don't know what you mean by I tried use function exists since you have not included this.我不知道你的意思是我试过使用 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
);
You can rewrite the query as below to get the desired output. Write a subquery to identify pn having ean13 as 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 = '')
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.