在SQLite中,我的查询:
DELETE FROM notification_invoice t1 WHERE notificationDate >= 1536883200000 and providerId in ("1234","5678")
AND EXISTS (
SELECT 1 FROM notification_invoice t2 WHERE
providerId in ("1234","5678")
and t2.notificationDate = t1.notificationDate
and t1.ownerKey = t2.ownerKey
AND t1._id < t2._id
)
但是我得到了错误:
Error: [SQLITE_ERROR] SQL error or missing database (near "t1": syntax error)
SQLState: null
ErrorCode: 1
答案 0 :(得分:1)
在要删除的表上删除别名的使用将解决语法错误。
DELETE FROM notification_invoice
WHERE notificationDate >= 1536883200000
AND providerId in ("1234","5678")
AND EXISTS (
SELECT 1
FROM notification_invoice t2
WHERE t2.providerId in ("1234","5678")
AND t2.notificationDate = notification_invoice.notificationDate
AND t2.ownerKey = notification_invoice.ownerKey
AND t2._id > notification_invoice._id
)
由于您没有描述要实现的数据或索引逻辑,因此无法说出逻辑是否正确。
也许子查询应该具有t2.providerId = notification_invoice.provider_id
。在不知道数据,约束,预期逻辑等的情况下我们无法分辨。
答案 1 :(得分:1)
Sqlite 确实支持带有DELETE
的表别名,您使用的语法错误。您需要在表名和别名之间使用AS
:
sqlite> CREATE TABLE foo(bar);
sqlite> INSERT INTO foo VALUES ('dog');
sqlite> SELECT * FROM foo;
bar
----------
dog
sqlite> DELETE FROM foo AS f WHERE f.bar = 'dog';
sqlite> SELECT * FROM foo;
sqlite>
如果您查看documentation for DELETE中的语法图,尤其是 qualified-table-name 语法图,您会发现AS
不是可选的就像它在SELECT
表名中一样。
答案 2 :(得分:0)
DELETE
语句在单个表上运行,不能使用表别名。别名导致您的错误。
有关类似情况,请参见stackoverflow.com/a/15832338/2577062。