Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用join、groupby和having caluse的Mysql更新表_Mysql_Mysql 5.6 - Fatal编程技术网

使用join、groupby和having caluse的Mysql更新表

使用join、groupby和having caluse的Mysql更新表,mysql,mysql-5.6,Mysql,Mysql 5.6,我对更新联接表有问题。我确实已经成功地选择了预期的结果,但是当我尝试更新时,总是出现错误 选择查询 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`

我对更新联接表有问题。我确实已经成功地选择了预期的结果,但是当我尝试更新时,总是出现错误

选择查询

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;

错误:在5.6中使用“按s.id分组并具有s.total average”的语法,根据的手册页,多表更新有限制,包括不按订单

平均值仅存在于SELECT查询中,未在update查询中定义

您需要包括一个子查询,以扩展您的where-like:

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 ...)`

问一个显而易见的问题,当你更新的时候,错误是什么?什么MySQL版本?@danblack嗨,我更新了帖子。