带有 WHERE 子句和 INNER JOIN 的 MySQL 更新查询不起作用

MySQL update query with WHERE clause and INNER JOIN not working(带有 WHERE 子句和 INNER JOIN 的 MySQL 更新查询不起作用)
本文介绍了带有 WHERE 子句和 INNER JOIN 的 MySQL 更新查询不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

但我不断收到 Not unique table/alias: 'sales_flat_order' 错误消息.有人能指出我正确的方向吗?

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

推荐答案

查询应该如下,你已经加入了同一个表,因此存在唯一别名的问题.我添加了表格别名以提高可读性.

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

这篇关于带有 WHERE 子句和 INNER JOIN 的 MySQL 更新查询不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)