如何从order_detail表中检索max product_id count?

时间:2017-09-04 10:00:23

标签: mysql sql

select max(count(od_product_id)) as no from order_detail group by od_product_id

在查询上方不起作用。

2 个答案:

答案 0 :(得分:1)

您正尝试按要汇总的列进行分组

试试这个:

select max(counted) 
from
(
select od_product_id, count(*) as counted
from order_detail
group by od_product_id
) x1

答案 1 :(得分:0)

您可以将HAVING用于此

SELECT od_product_id, COUNT(od_product_id) AS c
FROM order_detail 
GROUP by od_product_id
HAVING count(od_product_id) >= ALL
(
    SELECT count(od_product_id)
    FROM order_detail 
    GROUP by od_product_id
)