열 필드가 널인 테이블 행에서 삭제


11

어떤 열이 null인지 명시 적으로 지정하지 않고 열 필드가 null 인 테이블에서 행을 삭제하는 방법이 있습니까?

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 | 

감사

답변:


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;

모든 열이 null이 아닌 행만 반환합니다 . 당신은 반대를 원하기 때문에 where not (the_table is not null)조건 where the_table is null이 다르다는 것을 부정해야합니다 . 모든 열이 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 데모

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.