我有以下查询:SQL max函数
SELECT q.category_id as Category_id , COUNT(q.question_id) as count
from questions as q
INNER JOIN interestingQuestion as i using (question_id)
group by q.category_id
这是给我下面的结果 - 就像我需要根据我在我的表中的数据:
Category_id Count
5 1
6 3
现在我需要找到具有最高计数器CATEGORY_ID,所以我做了以下查询:
SELECT t.Category_id, MAX(t.Count)
from(
SELECT q.category_id as Category_id , COUNT(q.question_id) as count
from questions as q INNER JOIN interestingQuestion as i using (question_id)
group by q.category_id
)as t
和那我得到的结果是:
category_id MAX(t.count)
5 3
这是一个混合起来的结果,它的发现最大计数器,但它给我一个错误的CATEGORY_ID
为什么会发生?我该如何解决它?
Tnx!这是行之有效的,是否有可能通过这种方式返回一些结果(所有结果都与MAX相同)(不知道有多少结果)? – 2013-05-05 15:22:59
@ShaiZarzewski我已经更新了我的答案,现在应该没问题! – fthiella 2013-05-05 15:28:22
太棒了!它的工作很好,只需在最后一行之前的最后一行添加“as”,以便它能够工作 – 2013-05-05 15:41:04