SELECT ID拥有最多的ID号 [英] SELECT id HAVING maximum count of id
本文介绍了SELECT ID拥有最多的ID号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这样做不成功:
SELECT color_id
FROM产品
WHERE item_id = 1234
GROUP BY item_id
HAVING MAX(COUNT(color_id))
与
群组函数的使用无效
$ b $ SELECT color_id,COUNT(color_id)
FROM产品
WHERE item_id = 1234
GROUP BY item_id
返回 p>
color_id count
1,323
2,122
3,54
我正在寻找color_id 3,它的实例最多。
解决方案
<$ c $有没有一种快速简单的方法来获得我想要的内容? c> SELECT color_id AS id,COUNT(color_id)AS计数
FROM产品
WHERE item_id = 1234 AND color_id不为空
GROUP BY color_id
ORDER BY计数DESC
极限1;
这会为您提供color_id和color_id的计数,顺序由从最大到最小的计数排序。我认为这就是你想要的。
为您编辑...
SELECT color_id,COUNT(*)FROM products WHERE color_id = 3;
Have a products table with item_id and color_id. I'm trying to get the color_id with the most non-null instances.
This fails:
SELECT color_id
FROM products
WHERE item_id=1234
GROUP BY item_id
HAVING MAX(COUNT(color_id))
with
Invalid use of group function
This
SELECT color_id, COUNT(color_id)
FROM products
WHERE item_id=1234
GROUP BY item_id
Returns
color_id count
1, 323
2, 122
3, 554
I am looking for color_id 3, which has the most instances.
Is there a quick and easy way of getting what I want without 2 queries?
解决方案
SELECT color_id AS id, COUNT(color_id) AS count
FROM products
WHERE item_id = 1234 AND color_id IS NOT NULL
GROUP BY color_id
ORDER BY count DESC
LIMIT 1;
This will give you the color_id and the count on that color_id ordered by the count from greatest to least. I think this is what you want.
for your edit...
SELECT color_id, COUNT(*) FROM products WHERE color_id = 3;
这篇关于SELECT ID拥有最多的ID号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文