MySQL update query with WHERE clause and INNER JOIN not working

那年仲夏 提交于 2019-12-22 08:17:12

问题


Can't seem to reach the next step in my update query. I'm able to successfully view columns related to the select no problem:

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

This shows 3 columns all where related to the correct increment_id.

The next step is to update the sales_flat_order.coupon_code field. Here is my attempt:

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

But I keep getting a Not unique table/alias: 'sales_flat_order' error message. Could someone point me in the right direction?


回答1:


The query should be as below, you have joined the same table and hence the problem of unique alias. I have added table alias for better readability.

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



回答2:


You need to update sales_flat_order and join on sales_flat_order_grid - you've joined on 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";



回答3:


Yo have two times sales_flat_order, change with sales_flat_order_grid

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

If you want to join with the same table, use ALIAS

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


来源:https://stackoverflow.com/questions/26018065/mysql-update-query-with-where-clause-and-inner-join-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!