Py学习  »  DATABASE

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

Bebekim Lim • 5 年前 • 1545 次点击  

选择查询

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
 
1545 次点击  
文章 [ 1 ]  |  最新文章 5 年前