About MySQL GROUP BY statement

GROUP BY statement grouping result set according to one or more columns. On the grouped columns we can use COUNT, SUM, AVG, and other functions.

E.g:

CREATE TABLE `employee_tbl` (
  `id` int(11) NOT NULL, `name` char(10) NOT NULL DEFAULT '', `date` datetime NOT NULL, `singin` tinyint(4) NOT NULL DEFAULT '0' COMMENT '登录次数', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The INTO the INSERT `employee_tbl` the VALUES ( '1' , 'Bob' , '2016-04-22 15:25:33' , '1' ), ( '2' , 'Wang' , '2016-04-2015 : 25: 47 ' , ' 3 ' ), ( ' 3 ' , ' Mary ' , ' 2016-04-19 15:26:02 ' , ' 2 ' ), ( ' 4 ' , ' Wang ' , ' 2016-04-07 15:26:14 ' , ' 4 ' ), ( ' 5 ' , ' Bob ' ,'2016-04-11 15:26:40','4'),(    




'6' , 'Hsiao Ming' , '2016-04-04 15:26:54' , '2' );

1.group by can implement a simple query to the weight, assuming that there is to see under which employees, except with distinct, you can also use:

SELECT name FROM employee_tbl GROUP BY name;

  The result set returned is the name of all employees.

2. After the packet using the conditions defined HAVING, WHERE is original data conditions. Several order of the keywords is used where, group by, having, order by, for example:

SELECT name ,sum(*)  FROM employee_tbl WHERE id<>1 GROUP BY name HAVING sum(*)>5 ORDER BY sum(*) DESC;

 

Guess you like

Origin www.cnblogs.com/ZJOE80/p/10935291.html