分组和计数但只检索一列

时间:2016-03-14 05:22:21

标签: sql

以下SQL代码返回两列:' product_id'和'反击' 我希望它只返回' product_id',是否可能?

select product_id, count(*) as counter
from accounts_products
group by product_id
order by counter DESC
limit 1

1 个答案:

答案 0 :(得分:3)

只需删除counter列,然后直接ORDER BY计数:

SELECT product_id
FROM accounts_products
GROUP BY product_id
ORDER BY COUNT(*) DESC
LIMIT 1