ใช้ GROUP BY และ ORDER BY หาโดเมนที่มี Search Volume สูงสุด
สร้างตารางและเพิ่มข้อมูล
mysql> create table DemoTable (
-> URL varchar(40),
-> DomainName varchar(20),
-> SearchTimes int
-> );
Query OK, 0 rows affected (0.62 sec)
mysql> insert into DemoTable values('www.gmail.com','gmail.com',4);
Query OK, 1 row affected (0.26 sec)
mysql> insert into DemoTable values('www.google.com','google.com',3);
Query OK, 1 row affected (0.25 sec)
mysql> insert into DemoTable values('www.gmail.com','gmail.com',9);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('www.facebook.com','facebook.com',8);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('www.facebook.com','facebook.com',2);
Query OK, 1 row affected (0.16 sec)
คิวรี่รวม SearchTimes ตาม Domain แล้วเรียงมากไปน้อย
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)
gmail.com: 4+9=13 | facebook.com: 8+2=10 | google.com: 3 | ใช้ GROUP BY รวมกลุ่ม แล้ว ORDER BY SUM DESC