我如何:从结果中排序COUNT(column1):SELECT DISTINCT column1,column2

时间:2013-09-12 00:35:35

标签: mysql sql

因为标题可能不好,而且我对SQL非常弱,所以我会试着澄清我的目标:

如果:

SELECT DISTINCT host, author FROM t;

看起来像:

foo.com | John
bar.com | Bob
bar.com | Alice

我如何得到如下结果:

foo.com | 1
bar.com | 2

如果这还不够明确,请告诉我,我会更新或回复评论。

1 个答案:

答案 0 :(得分:2)

您需要group by,而不是select distinct

SELECT host, count(*)
FROM t
GROUP BY host
ORDER BY count(*);

如果您可能有重复的主机/作者组合,那么您可能需要count(distinct author)

SELECT host, count(distinct author)
FROM t
GROUP BY host
ORDER BY count(distinct author);