Sütun alanlarından herhangi birinin boş olduğu tablo satırlarından sil


12

Hangi sütunun null olduğunu açıkça belirtmeden sütun alanının herhangi birinin boş olduğu bir tablodan satır silmenin bir yolu var mı?

PostgreSQL kullanıyorum.

İşte benim ilişki şeması:

  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 | 

Teşekkürler

Yanıtlar:


19

Bunu yapmanın iki yolunu görüyorum:

Düz standart SQL ile, tüm sütunları listeleyin ve bunu bir OR ile birleştirin:

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;

Başka bir (Postgres'e özgü) çözüm, tüm satırın NOT NULL

select *
from the_table
where the_table is not null;

yalnızca tüm sütunların boş olmadığı satırları döndürür . O olumsuzladığı gerekir, böylece tersini istiyorum where not (the_table is not null)koşul where the_table is nullsatırları maçları - birşeylerindeğiştiğini tüm sütunları boş.

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

Teşekkür ederim! Bence ikinci çözüm aradığım çözüm.
dhaliman


Açık ve özlü where not (the_table is not null);yaklaşımı gerçekten çok seviyorum . Genel olarak SQL hakkında düşünebildiğim en iyisi NATURAL JOIN.
lad2025

0

Kullanabileceğiniz her bir sütunu belirtmek istemiyorsanız NOT EXISTS ... NATURAL JOIN.

Uyarı! Bu çözüm en iyi performans açısından değildir. Oracle / PostgreSQL / SQLite / MariaDB 10.3.2 ve üstü üzerinde çalışmalıdır.

Kurulum:

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  |
+----+-------------+---------+--------------+------+------+-------+-------+

Ve sorgu:

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

Çıktı:

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

DBFiddle Demosu

Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.