SQLite update query - subquery with aliases doesn&

2019-05-06 23:47发布

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?

1条回答
混吃等死
2楼-- · 2019-05-07 00:14

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'
查看更多
登录 后发表回答