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

dha*_*man 13 postgresql

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

我正在使用 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 | 
Run Code Online (Sandbox Code Playgroud)

谢谢

a_h*_*ame 21

我看到了两种方法:

使用普通标准 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;
Run Code Online (Sandbox Code Playgroud)

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

select *
from the_table
where the_table is not null;
Run Code Online (Sandbox Code Playgroud)

将只返回所有列都不为空的行。你想要相反的,所以你需要否定where not (the_table is not null)条件where the_table is null是不同的 - 只匹配所有列都为空的行。

delete from the_table
where not (the_table is not null);
Run Code Online (Sandbox Code Playgroud)

  • 这很巧妙 (3认同)