从带有域记录和搜索量的MySQL表中找出流行域
为此,可以将GROUPBY与ORDERBY子句一起使用。让我们首先创建一个表&mins;
mysql> create table DemoTable -> ( -> URL varchar(40), -> DomainName varchar(20), -> SearchTimes int -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('www.gmail.com','gmail.com',4); mysql> insert into DemoTable values('www.google.com','google.com',3); mysql> insert into DemoTable values('www.gmail.com','gmail.com',9); mysql> insert into DemoTable values('www.facebook.com','facebook.com',8); mysql> insert into DemoTable values('www.facebook.com','facebook.com',2);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+------------------+--------------+-------------+ | URL | DomainName | SearchTimes | +------------------+--------------+-------------+ | www.gmail.com | gmail.com | 4 | | www.google.com | google.com | 3 | | www.gmail.com | gmail.com | 9 | | www.facebook.com | facebook.com | 8 | | www.facebook.com | facebook.com | 2 | +------------------+--------------+-------------+ 5 rows in set (0.00 sec)
这是通过仅显示搜索量最高的域来查找流行域的查询。将为每个域计算搜索时间,并显示搜索量最大的域-
mysql> select DomainName,sum(SearchTimes) as TotalSearch from DemoTable -> group by DomainName -> order by TotalSearch desc;
这将产生以下输出-
+--------------+-------------+ | DomainName | TotalSearch | +--------------+-------------+ | gmail.com | 13 | | facebook.com | 10 | | google.com | 3 | +--------------+-------------+ 3 rows in set (0.00 sec)