mysql:使用子查询更新,

编程入门 行业动态 更新时间:2024-10-27 20:27:26
本文介绍了mysql:使用子查询更新,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个带有select语句的更新查询,该查询单独起作用.它将它用于无法正常工作的更新.

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);

我收到错误

您无法在FROM子句中指定要更新的目标表'data'"

"You can't specify target table 'data' for update in FROM clause"

因此,在深入研究之后,我发现我可以编写包括另一个select语句的内容:

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);

我收到错误

子查询返回多于1行"

"Subquery returns more than 1 row "

因此,在进一步挖掘之后,我添加了一个"ANY",因为这是常见的建议:

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);

并获得

您的SQL语法有误;请查看手册对应于您的MySQL服务器版本以使用正确的语法在'((从node.title = biblio_上的节点内部联接数据中选择nid)附近在第1行"

"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 "

我想念什么?

推荐答案

如果要更新 data 表中的所有行,则可以执行以下操作:

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

注意:

如果 node 中有多行具有相同的 title 值,则该行与 data ,则不确定要从哪个行中分配 nid 的值.

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.

如果 data 表中存在 name 值,而在 node 表中(在 title 列),则会将NULL值分配给 id 列.

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 )

更多推荐

mysql:使用子查询更新,

本文发布于:2023-06-01 10:31:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/413027.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:mysql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!