Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite update query - subquery with aliases doesn't work

I need to update a SQLite table.

The table looks like:

ID   | Address            | CallNumber   |  RefID
-----+--------------------+-------------------------------------------
ef78 | library            | 2002/13      | 100002
no56 | Lit                | 0189         | 100003
rs90 | temp               |              | 100003

For every column with Address = "Lit" there is a column Address = 'temp' with the same RefID. Now I need to update each Address = "temp" with the value "CallNumber" from the column with the same RefID.

The updated table should look like:

ID   | Address            | CallNumber   |  RefID
-----+--------------------+-------------------------------------------
ef78 | library            | 2002/13      | 100002
no56 | Lit                | 0189         | 100003
rs90 | 0189               |              | 100003

I tried this:

UPDATE Location
SET address = foo.callnumber
FROM (select RefID, CallNumber FROM Location) foo
WHERE foo.RefID=Location.RefID AND Location.Address = 'temp';

But all I got is a syntax error near "from".

Any clue?

like image 223
user2145165 Avatar asked Mar 19 '13 11:03

user2145165


People also ask

Can we use update in subquery?

UPDATE operations with subqueries that reference the same table object are supported only if all of the following conditions are true: The subquery either returns a single row, or else has no correlated column references. The subquery is in the UPDATE statement WHERE clause, using Condition with Subquery syntax.

Does SQLite support subquery?

In SQLite, a subquery is a query within a query. You can create subqueries within your SQL statements. These subqueries can reside in the WHERE clause, the FROM clause, or the SELECT clause.

Does SQLite support CTE?

SQLite doesn't support CTEs, window functions, or any of the like.

Which command is used to modify records SQLite?

There are three commands in data manipulation language group: INSERT: This command is used to create a record. UPDATE: It is used to modify the records. DELETE: It is used to delete records.


1 Answers

UPDATE commands do not have a FROM clause.

Use a correlated subquery:

UPDATE Location
SET Address = (SELECT CallNumber
               FROM Location L2
               WHERE L2.RefID = Location.RefID
                 AND L2.Address = 'Lit')
WHERE Address = 'temp'
like image 175
CL. Avatar answered Sep 26 '22 14:09

CL.