删除其他表中不存在的代码行

时间:2017-03-02 10:15:13

标签: sql sqlite

我有3个表机场,位置和位置提示。更新位置表后,我需要删除机场名称,这些名称不再出现在位置的n_airports列中,其中n_airpots列是由昏迷分隔的机场代码的集合。我试图逐个删除Lik%code%,但它没有用 这个陈述似乎删除了所有内容

DELETE
FROM location_hint 
WHERE type = 2 AND name NOT IN
(
    SELECT a.name
    FROM  airports a
    WHERE a.code IN
    (
        SELECT n_airports
        FROM location

        )
)

机场表

CREATE TABLE airports (
    _id INTEGER,
    code TEXT,
    name TEXT,
    PRIMARY KEY(_id)
)

enter image description here 位置表

CREATE TABLE `location` (
    `_id`   integer,
    `country`   text,
    `city`  text,
    `n_airports`    text,
    PRIMARY KEY(_id)
)

enter image description here 位置提示表

CREATE TABLE "location_hint" (
    `_id`   INTEGER PRIMARY KEY AUTOINCREMENT,
    `name`  TEXT NOT NULL,
    `type`  INTEGER NOT NULL,
    `country`   TEXT,
    `city`  TEXT
)

enter image description here

1 个答案:

答案 0 :(得分:2)

未经测试,在记事本上快速涂鸦 - 但请测试一下

DELETE
FROM location_hint 
WHERE type = 2 AND name IN
(
     SELECT a.name
    FROM  airports
    MINUS
    SELECT a.name
    FROM  airports a
    WHERE EXISTS(
        SELECT 1
        FROM location
        WHERE INSTR(','||n_airports||',' , ','||a.code||',') > 0
        )
)