小编典典

SQLite是否支持“从……删除”。

sql

这在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

阅读 208

收藏
2021-04-14

共1个答案

小编典典

通常,将整个联接移到一个子查询中,该子查询查找要删除的行的主键:

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)
2021-04-14