Does SQLite support “delete from from”

谁说我不能喝 提交于 2019-12-02 13:26:53

问题


This is valid syntax on Microsoft SQL Server's T-SQL, but not in SQLite, is there an alternative syntax that does the same in 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

回答1:


In the general case, move the entire join into a subquery that looks up the primary keys of the rows to be deleted:

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)

If you have a composite primary key, you can use the rowid instead:

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)

If you have a composite primary key and you created the table as a WITHOUT ROWID table, you have to rewrite the join as a correlated subquery:

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



回答2:


Here you have an alternative:

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


来源:https://stackoverflow.com/questions/24067581/does-sqlite-support-delete-from-from

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!