如何删除表内连接与Sqlite中的其他表?

时间:2014-07-01 13:10:03

标签: sqlite

我的查询:

DELETE a FROM TR_ContactResultRecord  a
INNER JOIN TR_Case  b on (a.FireStationCode=b.FireStationCode and a.CaseNo=b.CaseCode )
WHERE b.Update_DateTime <=20140628134416

显示错误:[Err] 1 - near "a": syntax error

如何在Sqlite中使用其他表删除表内连接?

2 个答案:

答案 0 :(得分:27)

尝试使用子查询重写查询:如果TR_ContactResultRecord的PK为CaseNo

DELETE FROM TR_ContactResultRecord
WHERE CaseNo IN (
  SELECT CaseNo FROM TR_ContactResultRecord a
  INNER JOIN TR_Case b
    ON (a.FireStationCode=b.FireStationCode and a.CaseNo=b.CaseCode )
  WHERE b.Update_DateTime <=20140628134416
);

答案 1 :(得分:3)

SQLite默认情况下在每个表中都有特殊的ROWID列。来自官方documentation

您可以使用特殊列名称ROWID,_ROWID_或OID之一访问SQLite表的ROWID。除非您声明普通表列使用那些特殊名称之一,否则该名称的使用将引用已声明的列而不是内部ROWID。

代码如下:

DELETE FROM TR_ContactResultRecord
WHERE ROWID IN (
  SELECT a.ROWID FROM TR_ContactResultRecord a
  INNER JOIN TR_Case b
    ON (a.FireStationCode=b.FireStationCode and a.CaseNo=b.CaseCode )
  WHERE b.Update_DateTime <=20140628134416
);