1

我目前的项目基本上是从 Facebook 导入我的朋友列表,然后选择频率最高的名字,即最常见的名字。我一直在尝试像这样设置子查询::

SELECT COUNT(*) as count, first_name 
FROM Friends GROUP BY first_name ORDER BY count DESC;

然后我被难住了......我一直在尝试在 where 子句中使用 MAX 函数,但它不会编译,所以我尝试将它放在子查询中,但我仍然无法得到它工作。我需要使用联接吗?

4

1 回答 1

2
SELECT   first_name
FROM     friends
GROUP BY first_name
ORDER BY COUNT(*) DESC
LIMIT 1

or this, that could return more than one row if more than one name has the maximum number of repetitions:

SELECT first_name
FROM friends
GROUP BY first_name
HAVING COUNT(*) = (SELECT COUNT(*) FROM FRIENDS
                   GROUP BY first_name ORDER BY COUNT(*) DESC LIMIT 1)
于 2013-07-04T16:11:05.663 回答