Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How delete table inner join with other table in Sqlite?

Tags:

sqlite

My query:

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

It show error: [Err] 1 - near "a": syntax error

How delete table inner join with other table in Sqlite?

like image 808
D T Avatar asked Jul 01 '14 13:07

D T


People also ask

Can we use DELETE with inner join?

MySQL also allows you to use the INNER JOIN clause in the DELETE statement to delete rows from a table and the matching rows in another table.

How do I DELETE from multiple tables using inner join in SQL Server?

DELETE JOIN is an advanced structured query language(SQL) statement that is used to perform delete operations in multiple tables while using SQL JOIN such that all rows are deleted from the first table and the matching rows in another table or based on the kind of join operation used in the query.

How do you DELETE a record from one table that matches another in SQL?

You can specify the table name to remove data using the delete statement. Both the SQL statements are the same. We can specify the table name from the (optional) keyword or specify the table name directly after the delete.


2 Answers

Try to rewrite you query using subquery: In case your PK for TR_ContactResultRecord is 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
);
like image 195
Oleksandr Papchenko Avatar answered Oct 17 '22 23:10

Oleksandr Papchenko


SQLite has special ROWID column by default in each table. From official documentation:

You can access the ROWID of an SQLite table using one of the special column names ROWID, _ROWID_, or OID. Except if you declare an ordinary table column to use one of those special names, then the use of that name will refer to the declared column not to the internal ROWID.

The code is following:

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
);
like image 19
Peter Trcka Avatar answered Oct 17 '22 23:10

Peter Trcka