MySQL之查询语句的基本操作 (2)

总结: 聚合函数,必须跟在group by 后面(执行顺序);

# 聚合函数: 若没有group by 分组,默认将查出来的数据当做一个分组, 也能使用; select max(salary) from emp; # group_concat(name): 可以将分组后的 所有名字获取并进行拼接 # 默认以, 拼接 select post, group_concat(name) from emp group by post; # 指定以:拼接 select post, group_concat(name, \':\') from emp group by post; # select post, group_concat(\'Name: \', name) from emp group by post; select post, group_coucat(name) from emp group by post; mysql> select post, group_concat(name) from emp group by post; +-----------------------------------+------------------------------------------------+ | post | group_concat(name) | +-----------------------------------+------------------------------------------------+ | NULL | o | | operation | 程咬铁,程咬铜,程咬银,程咬金,张野 | | sale | 格格,星星,丁丁,丫丫,歪歪 | | teacher | sean,大饼,jerry,owen,jason,kevin,egon | | 张江第一帅形象代言部门 | tank | +-----------------------------------+------------------------------------------------+ 5 rows in set (0.00 sec) # 给每个部门的员工名字前 + NB_ select post, group_concat(\'NB_\', name) from emp group by post; # 拼接部门员工名字+薪资 select post, group_concat(name, ":", salary) from emp group by post;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzfwgj.html