社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  DATABASE

带有join、groupby和having caluse的Mysql更新表

Bebekim Lim • 5 年前 • 1410 次点击  

选择查询

select sale_id, price,cONVERT(sum(price) , decimal(10,2)) as average, s.total from sale_items i
JOIN sales s ON s.id = i.sale_id
where s.currency = 'USD' 
and s.currency_total != 0 
and s.`deleted_at` is null
and i.`deleted_at` is null
group by s.id
having s.total <> average;  

更新我尝试的查询

    UPDATE sale_items i
JOIN sales s ON s.id = i.sale_id

SET i.price=(i.price / (s.total/s.currency_total)), i.total=(i.total / (s.total/s.currency_total)), i.total_tax=(i.total_tax / (s.total/s.currency_total))
where s.currency = 'USD' 
and s.currency_total != 0 
and s.`deleted_at` is null
and i.`deleted_at` is null
group by s.id
having s.total <> average;

错误:在'group by s.id having s.total<gt;average'附近使用的语法

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/54053
 
1410 次点击  
文章 [ 1 ]  |  最新文章 5 年前
danblack
Reply   •   1 楼
danblack    5 年前

在5.6中,按照 UPDATE ,多表更新有限制,包括 ORDER BY

average 只存在于你 SELECT 查询,它未在更新查询中定义。

您将需要包含一个子查询来扩展where,如下所示:

UPDATE sale_items i
JOIN sales s ON s.id = i.sale_id
SET i.price=(i.price / (s.total/s.currency_total)), i.total=(i.total / (s.total/s.currency_total)), i.total_tax=(i.total_tax / (s.total/s.currency_total))
where s.currency = 'USD' 
and s.currency_total != 0 
and s.`deleted_at` is null
and i.`deleted_at` is null
and s.total <> (select sum(price) from sale_items WHERE sale_id = s.id AND ...)`