[英]mysql: update with subquery,
I have an update query with a select statement, which separately works. 我有一个带有select语句的更新查询,该查询单独起作用。 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"
“无法在FROM子句中指定目标表'data'进行更新”
So, after digging around, I found that I could write include another select statement: 因此,深入研究之后,我发现可以编写包括另一个select语句的内容:
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 "
“子查询返回多于1行”
So after more digging, I added an "ANY", as this is the common recommendation: 因此,在进一步挖掘之后,我添加了一个“ ANY”,因为这是常见的建议:
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 "
“您的SQL语法有错误;请在与MySQL服务器版本相对应的手册中找到正确的语法,以在'(第1行的node.title = biblio_'的节点内部联接数据中选择nid”附近使用“
What am I missing? 我想念什么?
If you want to update all rows in the data
table, you can do something like this: 如果要更新
data
表中的所有行,可以执行以下操作:
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. 如果
node
中有多行具有相同的title
值(与data
中的name
相匹配),则不确定nid
的值将nid
几行中分配。
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. 如果
data
表中有name
值,而在node
表( title
列)中找不到,则会将空值分配给id
列。
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
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.