MySQL - 使用 INNER SELECT 进行更新
我想做这样的事情,但似乎我做不到:
UPDATE products p2
SET
manufacturer =
(
SELECT manufacturer
FROM products p, manufacturers m, etc.
WHERE
...other stuff...
p.name LIKE CONCAT('%',m.name,'%')
AND p.id = p2.id
)
WHERE manufacturer = 0
错误是: #1093 - 您无法在 FROM 子句中指定用于更新的目标表“p2”
问题是我在表中有一个制造商字段,但某些制造商名称位于产品名称字符串而不是制造商字段中,因此我必须通过产品名称获取制造商 ID 并更新产品表其制造商 ID。
我可以分两步完成,但我喜欢一步完成。
I want to do something like this, but it seems that i just dont can:
UPDATE products p2
SET
manufacturer =
(
SELECT manufacturer
FROM products p, manufacturers m, etc.
WHERE
...other stuff...
p.name LIKE CONCAT('%',m.name,'%')
AND p.id = p2.id
)
WHERE manufacturer = 0
The error is:
#1093 - You can't specify target table 'p2' for update in FROM clause
The problem is that i have a manufacturer field in the table, but some of the manufactureres name are in the product_name-string instead of the manufacturer field, so i have to get the manufacturer id by the product_name and update the product-table its manufacturer id.
I could do this in two steps, but id like to do this in one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试这个查询 -
...但首先检查记录是否正确连接 -
Try this query -
...but firstly check that records are joined correctly -
关于此相同错误的评论位于 http://dev.mysql。 com/doc/refman/5.0/en/subquery-errors.html 说,“如果您同时使用子查询读取相同的数据,MySQL 不允许更新或删除表的数据。”
A comment regarding this same error at http://dev.mysql.com/doc/refman/5.0/en/subquery-errors.html says, "MySQL does not allow to UPDATE or DELETE a table's data if you're simultaneously reading that same data with a subquery."
一张有效表的示例:
此语句将所有 AKTIV 记录移动到 DEAKTIV
已经存在匹配的 DRAFT 记录:
执行上面的语句后,我只需将 DRAFT 记录更新为 ACTIV 并提交
Example on one table that works:
This statement moves all AKTIV-records to DEAKTIV
where already an matching DRAFT-record exists:
After executing the statement above i simply updtae the DRAFT-records ton ACTIV and commit
看看这种查询是否适合您
您可能需要修改上面的查询,因为我根据我的假设编写了查询
See if this kind of query works for you
You may have to modify above query as i have written on the basis of my assumption