Mysql “如何避免错误”;WHERE";中不允许使用聚合函数;
此sql代码抛出一个 其中不允许使用聚合函数Mysql “如何避免错误”;WHERE";中不允许使用聚合函数;,mysql,sql,aggregate-functions,Mysql,Sql,Aggregate Functions,此sql代码抛出一个 其中不允许使用聚合函数 如何避免此错误?将WHERE子句替换为HAVING,如下所示: SELECT o.ID , count(p.CAT) FROM Orders o INNER JOIN Products p ON o.P_ID = p.P_ID GROUP BY o.ID HAVING count(p.CAT) > 3; HAVING类似于WHERE,两者都用于过滤结果记录,但HAVING用于过滤聚合数据(当使用GROUP BY时)。使用HAVING子句而
如何避免此错误?将
WHERE
子句替换为HAVING
,如下所示:
SELECT o.ID , count(p.CAT)
FROM Orders o
INNER JOIN Products p ON o.P_ID = p.P_ID
GROUP BY o.ID
HAVING count(p.CAT) > 3;
HAVING
类似于WHERE
,两者都用于过滤结果记录,但HAVING
用于过滤聚合数据(当使用GROUP BY
时)。使用HAVING
子句而不是WHERE
试试这个:
SELECT o.ID, COUNT(p.CAT) cnt
FROM Orders o
INNER JOIN Products p ON o.P_ID = p.P_ID
GROUP BY o.ID HAVING cnt > 3
如果我们有条件列出的价格高于订单表中所列价格的中位数,那么self-join将与join进行折腾吗 例如:订购物品、订购价格 因为聚合函数不能在WHERE子句中使用>
select a.order_item_product_price, count(distinct
a.order_item_product_price) from
default.order_items a , default.order_items b
where a.order_item_product_price = b.order_item_product_price
group by a.order_item_product_price
having a.order_item_product_price > appx_median(b.order_item_product_price)
order by a.order_item_product_price limit 10
select a.order_item_product_price, count(distinct
a.order_item_product_price) from
default.order_items a , default.order_items b
where a.order_item_product_price = b.order_item_product_price
group by a.order_item_product_price
having a.order_item_product_price > appx_median(b.order_item_product_price)
order by a.order_item_product_price limit 10