简体   繁体   English

Sql 查询查找值为 null 的所有行,但具有相同值的另一行不是 null (Mariadb)

[英]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 = '')
);

DB Fiddle: Result DB 小提琴: 结果

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL查询 - 查找所有条目都为非空的行 - SQL Query- Find row where all entries are non Null sql 查询显示一组字段对于另一个字段中的所有行具有相同值的所有行 - sql query to show all rows where a group of a field has the same value for all rows in another field SQL查询以查找某列值等于同一行中其他两列之和的行 - SQL query to find rows where a column value is equal to the sum of tow other columns in the same row 当删除同一表中的另一行时,将行内的部分/全部值更新为null - Updating some/all value inside row to null when another row in same table is deleted MYSQL 按列ID选择行,如果包含null,则包含另一列中包含相同值的行 - Select row by column id AND include rows that contain the same value from another column if null MySQL-如何返回不同的ID,其中相同ID的所有行的字段值均为空 - MySQL - How to return distinct IDs where all rows for the same ID have null field value 我正在尝试查找具有空值作为外键的行,但所有列都不返回任何行 - I am trying to find a row with has null value as foreign key but all the columns return no rows 我怎样才能 select 所有不与另一行 null 共享列值的行? - How can I select all the rows which do not share a column value with another row which is null? MySQL Drop列,其中所有行值为null - Mysql Drop column where all row value is null 列的所有行都为null值! - null value for all rows of a column!
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM