[英]Select distinct values from a table and add 'count' column
Generally, our application for statistics records searches by IP. 通常,我们的统计记录应用程序按IP进行搜索。 That means - three IPs and one query, 3 entries in a database. 这意味着-三个IP和一个查询,一个数据库中有3个条目。
We need to show statistic of those searches, but imagine that we have: 我们需要显示这些搜索的统计信息,但是假设我们有:
query | count | ip
where 2 127.0.0.1
where 1 127.0.0.2
where 4 127.0.0.3
who 4 127.0.2.1
I need, in my rails application, to sort these 'where' as one query. 我需要在Rails应用程序中将这些“ where”排序为一个查询。 So, if I write Search.order('count DESC'), I need to get results like 因此,如果我编写Search.order('count DESC'),则需要获得类似
query | count
where 7
who 4
Does anyone have solution? 有人有解决方案吗?
Select query, sum(count) as SumCount from table group by query
Something like this will get you started 这样的事情会让你入门
select query, sum(count) thecount
from etc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.