[英]How do I: ORDER BY COUNT(column1) from the result of: SELECT DISTINCT column1, column2
Because the title is probably bad, and I'm very weak with SQL, I'll try to clarify my goal: 因为标题可能很糟糕,而且我对SQL非常弱,所以我会试着澄清我的目标:
if: 如果:
SELECT DISTINCT host, author FROM t;
Looks like: 看起来像:
foo.com | John
bar.com | Bob
bar.com | Alice
How do I get results like: 我如何获得如下结果:
foo.com | 1
bar.com | 2
Let me know if this isn't clear enough, and I will update, or respond to comments. 如果这还不够清楚,请告诉我,我会更新或回复评论。
You want a group by
, not select distinct
: 你想要一个
group by
,而不是select distinct
:
SELECT host, count(*)
FROM t
GROUP BY host
ORDER BY count(*);
If you might have duplicate host/author combinations, then you might want count(distinct author)
: 如果您可能有重复的主机/作者组合,那么您可能需要
count(distinct author)
:
SELECT host, count(distinct author)
FROM t
GROUP BY host
ORDER BY count(distinct author);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.