SQLite update query - subquery with aliases doesn't work

喜夏-厌秋 提交于 2019-12-07 06:47: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:


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'


来源:https://stackoverflow.com/questions/15498173/sqlite-update-query-subquery-with-aliases-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!