从表中任何列字段为空的行中删除


11

有没有一种方法可以从表中删除任何行字段为空的行,而无需明确指定哪一列为空?

我正在使用postgreSQL。

这是我的关系模式:

  Column    |  Type   |                              Modifiers                               
  --------------+---------+----------------------------------------------------------------------
  id           | integer | not null default  nextval('aurostat.visitor_center_id_seq'::regclass)
  date         | date    | 
  persons      | integer | 
  two_wheelers | integer | 
  cars         | integer | 
  vans         | integer | 
  buses        | integer | 
  autos        | integer | 

谢谢

Answers:


18

我看到两种方法:

使用普通标准SQL,只需列出所有列,然后将其与OR结合即可:

delete from the_table
where date is null
   or persons is null
   or two_wheelers is null
   or cars is null
   or vans is null
   or buses is null
   or autos is null;

另一种(特定于Postgres的)解决方案是将整行与 NOT NULL

select *
from the_table
where the_table is not null;

将仅返回所有列都不为空的行。您希望得到相反的结果,因此您需要否定where not (the_table is not null)条件where the_table is null是不同的-仅匹配所有列均为空的行。

delete from the_table
where not (the_table is not null);

谢谢!我认为第二个解决方案是我一直在寻找的解决方案。
dhaliman

3
那太巧妙了
杰克说尝试topanswers.xyz

我真的很喜欢简洁明了的where not (the_table is not null);方法。我能想到的一般SQL最好的就是NATURAL JOIN
lad2025

0

如果您不想指定每个列,则可以使用NOT EXISTS ... NATURAL JOIN

警告!从性能的角度来看,这种解决方案不是最好的。它应该可以在Oracle / PostgreSQL / SQLite / MariaDB 10.3.2及更高版本上运行。

配置:

CREATE TABLE the_table(
   id           integer not null 
  ,date_          date    
  ,persons       integer 
  ,two_wheelers  integer 
  ,cars          integer 
  ,vans          integer 
  ,buses         integer 
 , autos         integer 
);

INSERT INTO the_table(id, date_, persons, two_wheelers, cars, vans, buses, autos)
VALUES (1, '21/JAN/2018',1,1,1,1,1,1);

INSERT INTO the_table(id, date_, persons, two_wheelers, cars, vans, buses, autos)
VALUES (2, '21/JAN/2018',2,2,2,2,NULL,2);
INSERT INTO the_table(id, date_, persons, two_wheelers, cars, vans, buses, autos)
VALUES (3, '21/JAN/2018',3,3,3,3,NULL,NULL);

SELECT * FROM the_table;

+----+-------------+---------+--------------+------+------+-------+-------+
| id |    date_    | persons | two_wheelers | cars | vans | buses | autos |
+----+-------------+---------+--------------+------+------+-------+-------+
|  1 | 21/JAN/2018 |       1 |            1 |    1 |    1 | 1     | 1     |
|  2 | 21/JAN/2018 |       2 |            2 |    2 |    2 | null  | 2     |
|  3 | 21/JAN/2018 |       3 |            3 |    3 |    3 | null  | null  |
+----+-------------+---------+--------------+------+------+-------+-------+

并查询:

DELETE FROM the_table
WHERE NOT EXISTS (SELECT *
                  FROM the_table t1
                  NATURAL JOIN the_table t2
                  WHERE id = the_table.id);

输出:

+----+-------------+---------+--------------+------+------+-------+-------+
| id |    date_    | persons | two_wheelers | cars | vans | buses | autos |
+----+-------------+---------+--------------+------+------+-------+-------+
|  1 | 21/JAN/2018 |       1 |            1 |    1 |    1 |     1 |     1 |
+----+-------------+---------+--------------+------+------+-------+-------+

DBFiddle演示

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.