SQLite支持"从"删除

时间:2014-06-05 18:28:47

标签: sql sqlite sql-delete

这是Microsoft SQL Server的T-SQL上的有效语法,但在SQLite中没有,是否有另一种语法在SQLite中执行相同的操作?

DELETE FROM something
FROM something
INNER JOIN combinations ON something.field1=combinations.field1
AND something.field2=combinations.field2
--optionally more criteria and/or more joins
WHERE combinations.field3=1234
--or anything really, just to show some criteria could be applied here also

2 个答案:

答案 0 :(得分:2)

在这里你有另一种选择:

DELETE FROM slave
where slave.master_id in (select master.id from master where master.something=1234)

答案 1 :(得分:2)

在一般情况下,将整个联接移动到子查询中,该子查询查找要删除的行的主键:

DELETE FROM something
WHERE id IN (SELECT something.id
             FROM something
             JOIN combinations ON something.field1=combinations.field1
                              AND something.field2=combinations.field2
             WHERE combinations.something=1234)

如果您有复合主键,则可以改为使用rowid

DELETE FROM something
WHERE rowid IN (SELECT something.rowid
                FROM something
                JOIN combinations ON something.field1=combinations.field1
                                 AND something.field2=combinations.field2
                WHERE combinations.something=1234)

如果您有复合主键并且您将表创建为WITHOUT ROWID表,则必须将连接重写为相关子查询:

DELETE FROM something
WHERE EXISTS (SELECT 1
              FROM combinations
              WHERE field1 = something.field1
                AND field2 = something.field2
                AND field3 = 1234)