ลบออกจากแถวของตารางที่ใด ๆ ของเขตข้อมูลคอลัมน์เป็นโมฆะ


12

มีวิธีการลบแถวออกจากตารางที่ใด ๆ ของเขตข้อมูลคอลัมน์เป็นโมฆะโดยไม่ระบุชัดเจนว่าคอลัมน์ใดเป็นโมฆะ?

ฉันใช้ 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 | 

ขอบคุณ

คำตอบ:


19

ฉันเห็นสองวิธีในการทำเช่นนั้น:

ด้วย 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
นั่นคือความ
เฉลียวฉลาด

ฉันชอบวิธีการที่ชัดเจนและรัดกุม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

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.