MySQL - 使用 INNER SELECT 进行更新

发布于 2025-01-05 19:09:55 字数 421 浏览 0 评论 0原文

我想做这样的事情,但似乎我做不到:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梨涡 2025-01-12 19:09:55

尝试这个查询 -

UPDATE products p
  JOIN manufacturers m
    ON LOCATE(p.name, m.name) > 0
SET p.manufacturer = m.id;

...但首先检查记录是否正确连接 -

SELECT * FROM products p
  JOIN manufacturers m
    ON LOCATE(p.name, m.name) > 0 -- bind records in two tables

Try this query -

UPDATE products p
  JOIN manufacturers m
    ON LOCATE(p.name, m.name) > 0
SET p.manufacturer = m.id;

...but firstly check that records are joined correctly -

SELECT * FROM products p
  JOIN manufacturers m
    ON LOCATE(p.name, m.name) > 0 -- bind records in two tables
不必你懂 2025-01-12 19:09:55

关于此相同错误的评论位于 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."

挽清梦 2025-01-12 19:09:55

一张有效表的示例:

此语句将所有 AKTIV 记录移动到 DEAKTIV
已经存在匹配的 DRAFT 记录:

update table1 fa inner join table1 fe on (fa.datum = fe.datum and fa.firmenid = fe. firmenid and  fe.status = 'DRAFT')
set fa.status = 'DEACTIV'
where fa.firmenid = '+49151506292' and fa.status = 'ACTIV';

执行上面的语句后,我只需将 DRAFT 记录更新为 ACTIV 并提交

Example on one table that works:

This statement moves all AKTIV-records to DEAKTIV
where already an matching DRAFT-record exists:

update table1 fa inner join table1 fe on (fa.datum = fe.datum and fa.firmenid = fe. firmenid and  fe.status = 'DRAFT')
set fa.status = 'DEACTIV'
where fa.firmenid = '+49151506292' and fa.status = 'ACTIV';

After executing the statement above i simply updtae the DRAFT-records ton ACTIV and commit

蓝颜夕 2025-01-12 19:09:55

看看这种查询是否适合您

UPDATE products p2 INNER JOIN manufacturers m ON m.product_id = p2.id AND p2.name LIKE CONCAT('%',m.name,'%') SET manufacturer = YOURVALUE

您可能需要修改上面的查询,因为我根据我的假设编写了查询

See if this kind of query works for you

UPDATE products p2 INNER JOIN manufacturers m ON m.product_id = p2.id AND p2.name LIKE CONCAT('%',m.name,'%') SET manufacturer = YOURVALUE

You may have to modify above query as i have written on the basis of my assumption

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文