问题
I have an update query with a select statement, which separately works. It's using it for the update that's not working.
update data set data.id = (select nid from node inner join data on node.title = data.name);
I get the error
"You can't specify target table 'data' for update in FROM clause"
So, after digging around, I found that I could write include another select statement:
update data set data.id = (select nid from(select nid from node inner join data on node.title = data.name) AS temptable);
I get the error
"Subquery returns more than 1 row "
So after more digging, I added an "ANY", as this is the common recommendation:
update data set data.id = (select nid from ANY (select nid from node inner join data on node.title = data.name) AS temptable);
and get
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(select nid from node inner join data on node.title = biblio_' at line 1 "
What am I missing?
回答1:
If you want to update all rows in the data
table, you can do something like this:
UPDATE data
LEFT
JOIN node
ON node.title = data.name
SET data.id = node.nid
NOTES:
If there are multiple rows in node
with the same value for title
, which matches a name
in data
, it's indeterminate which of those rows the value of nid
will be assigned from.
If there are values of name
in the data
table which are not found in the node
table (in the title
column), then a NULL value will be assigned to the id
column.
Some tweaks to the query can modify this behavior.
It's possible to accomplish this using a subquery, but I would just use a join operation. I think you could use a correlated subquery, like this:
UPDATE data
SET data.id = ( SELECT node.nid
FROM node
WHERE node.title = data.name
ORDER BY node.nid
LIMIT 1
)
来源:https://stackoverflow.com/questions/35023658/mysql-update-with-subquery