MySQL更新查询与WHERE子句和INNER JOIN不起作用

时间:2014-09-24 13:22:01

标签: mysql inner-join where-clause

似乎无法进入更新查询的下一步。我能够成功查看与选择无问题相关的列:

SELECT sales_flat_order_grid.entity_id,sales_flat_order_grid.increment_id,sales_flat_order.coupon_code
FROM sales_flat_order_grid 
INNER JOIN sales_flat_order ON sales_flat_order_grid.entity_id = sales_flat_order.entity_id     
WHERE sales_flat_order_grid.increment_id = "12345678";

这显示了与正确的increment_id相关的3列。

下一步是更新sales_flat_order.coupon_code字段。这是我的尝试:

UPDATE sales_flat_order 
INNER JOIN sales_flat_order ON sales_flat_order_grid.entity_id = sales_flat_order.entity_id      
WHERE sales_flat_order_grid.increment_id = "12345678"
SET coupon_code = "newcoupon";

但我不断收到 不唯一的表/别名:' sales_flat_order' 错误消息。有人能指出我正确的方向吗?

3 个答案:

答案 0 :(得分:7)

查询应如下所示,您已加入同一个表,因此出现了唯一别名的问题。我添加了表别名以提高可读性。

UPDATE 
sales_flat_order sfo
INNER JOIN sales_flat_order_grid sfog 
ON sfog.entity_id = sfo.entity_id      
SET sfo.coupon_code = "newcoupon"
WHERE sfog.increment_id = "12345678" ; 

答案 1 :(得分:0)

您需要更新sales_flat_order并加入sales_flat_order_grid - 您已加入sales_flat_order

UPDATE sales_flat_order 
INNER JOIN sales_flat_order_grid 
      ON sales_flat_order_grid.entity_id = sales_flat_order.entity_id      
WHERE sales_flat_order_grid.increment_id = "12345678"
SET coupon_code = "newcoupon";

答案 2 :(得分:0)

哟有两次sales_flat_order,更改为sales_flat_order_grid

UPDATE sales_flat_order
INNER JOIN sales_flat_order_grid ... -- Need change the namme of the table

如果您想加入同一张桌子,请使用ALIAS

UPDATE sales_flat_order AS sfo1
INNER JOIN sales_flat_order AS sfo2 ...