我有两张桌子(A和B)。
我想删除表B中B.1不在表A.2中的所有行。
所以我在sqlite中写了这个公式:
DELETE FROM B
WHERE 1
IN
(SELECT *
FROM B
LEFT JOIN A
ON A.1=B.2
WHERE A.1
IS NULL)
但这会返回此错误:
only a single result allowed for a SELECT that is part of an expression
有人能帮我一把吗?
感谢。
答案 0 :(得分:4)
示例查询的问题是,当SELECT *
返回多个列时,IN子句不能与SELECT *
一起使用。您需要指定列...
DELETE FROM B
WHERE B.2 NOT IN (SELECT A.1
FROM A)
DELETE FROM B
WHERE NOT EXISTS (SELECT NULL
FROM A
WHERE A.1 = B.2)
SQLite不支持DELETE语句中的JOIN,但您也可以使用:
DELETE FROM B
WHERE B.2 IN (SELECT B.2
FROM B
LEFT JOIN A ON A.1 = B.2
WHERE A.1 IS NULL)
我没有SQLite的任何性能统计信息,但NOT EXISTS
将是我的选择,因为它在第一次满足时返回true - 非常适合处理重复项。
答案 1 :(得分:1)
我认为你的问题中有某个错字(也许是表B.2?),但我认为你想要的是:
DELETE FROM B WHERE NOT EXISTS (SELECT * FROM A WHERE A.1 = B.2)