相当基础;无效的群组功能。我知道我需要使用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';

最佳答案

如果没有分组依据,您将无法使用诸如count之类的聚合函数,并将其分配为结果
在这种情况下,您应该使用适当的子选择来获取要分配给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';

关于mysql - 这里的一些基本知识:无效的群组功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39775438/

10-12 07:35