基本的东西:无效的组功能

时间:2016-09-29 16:22:35

标签: mysql

相当基本;组功能无效。我知道我需要使用HAVING但不知道如何重写它。

update deals d 
join deal_products dp on dp.deal_id = d.id
set d.products_count = count(distinct product_id)
where dp.active_flag=1 and dp.enabled_flag=1 and d.status != 'deleted';

1 个答案:

答案 0 :(得分:0)

您不能使用聚合函数(如count而不使用group by)并将此值作为结果分配 在这种情况下,您应该使用适当的子选择来获取要在d.products_count分配的值

update deals d 
join deal_products dp on dp.deal_id = d.id
set d.products_count = (select count(distinct product_id) 
                         from  your_table  where Your_condition)
where dp.active_flag=1 and dp.enabled_flag=1 and d.status != 'deleted';