Home > Database > Mysql Tutorial > Group by字句的用法

Group by字句的用法

WBOY
Release: 2016-06-07 17:25:35
Original
1127 people have browsed it

--先按照job_id分组,再按照manager_id分组select job_id,manager_id,sum(salary),count(*) from employees group by job_id,man

--先按照job_id分组,再按照manager_id分组
select job_id,manager_id,sum(salary),count(*) from employees group by job_id,manager_id;

--先对job_id进行分组,输出按状态分组的结果,然后再按照job_id分组,并对分组后的job_id按照 manager_id进行再分组,输出结果;
--在第一行输出统计的总和(1=2+3=4+5+6+7+8+9)。即他是将同一结果集按照不同的分组条件分别输出了2次。
select job_id,manager_id,sum(salary),count(*)  from employees group by rollup(job_id,manager_id);

--先按照manager_id分组,输出行;然后输出合计行;然后按照job_id分组,输出行;然后在按照job_id和manager_id分组输出行。即,其实他是将同一结果集按照3种条件分别GROUP并输出了3次。
select job_id,manager_id,sum(salary),count(*)  from employees group by cube(job_id,manager_id);

--可见此时其作用等同于CUBE的前2次分组。缺失了第三次分组(即同时按照job_id和manager_id分组)和合计行。
select job_id,manager_id,sum(salary),count(*)  from employees group by grouping sets(job_id,manager_id);
--其实GROUP BY GROUPING SETS也是可以输出合计的,只需要加一对空括号即可:
select job_id,manager_id,sum(salary),count(*)  from employees group by grouping sets(job_id,manager_id,());

linux

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template