如果您不想指定每个列,则可以使用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演示