当另一个表中不存在匹配项时,从SQLite表中删除行 [英] Deleting rows from SQLite table when no match exists in another table
本文介绍了当另一个表中不存在匹配项时,从SQLite表中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从SQLite表中删除行,这些行的ID在另一个表中不存在. SELECT语句返回正确的行:
I need to delete rows from an SQLite table where their row IDs do not exist in another table. The SELECT statement returns the correct rows:
SELECT * FROM cache LEFT JOIN main ON cache.id=main.id WHERE main.id IS NULL;
但是,delete语句从SQLIte生成错误:
However, the delete statement generates an error from SQLIte:
DELETE FROM cache LEFT JOIN main ON cache.id=main.id WHERE main.id IS NULL;
错误是: SQLite错误1-靠近左":语法错误.还有其他可以使用的语法吗?
The error is: SQLite Error 1 - near "left": syntax error. Is there another syntax I could use?
推荐答案
SQLite显然不支持使用delete语句进行联接,正如您在
SQLite apparently doesn't support joins with the delete statement, as you can see on the Syntax diagrams. You should however be able to use a subquery to delete them.
即.
DELETE FROM cache WHERE id IN
(SELECT cache.id FROM cache LEFT JOIN main ON cache.id=main.id WHERE main.id IS NULL);
(未经测试)
这篇关于当另一个表中不存在匹配项时,从SQLite表中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文