[英]MySQL - Update with Subquery
i'm trying to write a MySQL query for update a table, but i obtain an error. 我正在尝试编写MySQL查询以更新表,但出现错误。 This is my query: 这是我的查询:
UPDATE mytable
SET mytable.email = (
select ps_customer.id_customer
from ps_customer
where ps_customer.email = mytable.email)
where (ps_customer.email = mytable.email)
Mysql says: #1054 - Unknown column 'ps_customer.email' in 'where clause' MySQL说:#1054-'where子句'中的未知列'ps_customer.email'
I can't understand where error is. 我不明白错误在哪里。 Can you help me, please? 你能帮我吗?
Best Regards, Simone 最好的问候,西蒙妮
Your second WHERE cannot see inside the subquery. 您的第二个WHERE无法在子查询中看到。 How you are approaching this is a bit odd; 您的处理方式有点奇怪。 if you use an UPDATE with an INNER JOIN, you can use your WHERE condition as the JOIN criteria and just SET the field in one JOINed table with a value from the other. 如果将UPDATE与INNER JOIN一起使用,则可以将WHERE条件用作JOIN条件,而只需将一个JOINed表中的字段设置为另一个中的值即可。 Like so.... 像这样...
UPDATE mytable INNER JOIN ps_customer
ON mytable.email = ps_customer.email
SET mytable.email = ps_customer.id_customer
;
You don't need the second WHERE
clause. 您不需要第二个WHERE
子句。 The WHERE
clause in the subquery matches the rows between the two tables (this is called a correlated subquery ). 子查询中的WHERE
子句匹配两个表之间的行(这称为关联子查询 )。 But a better way to write it is as a join: 但是写它的更好的方法是连接:
UPDATE mytable AS m
JOIN ps_customer AS c ON m.email = c.email
SET m.email = c.id_customer
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.