当前位置:Gxlcms >
数据库问题 >
mysql常用基础操作语法(七)--统计函数和分组查询【命令行模式】
mysql常用基础操作语法(七)--统计函数和分组查询【命令行模式】
时间:2021-07-01 10:21:17
帮助过:2人阅读
注:文中所有的。。。代表多个。
1、使用count统计条数:select count(字段名。。。) from tablename;


2、使用avg计算字段的平均值:select avg(字段名) from tablename;


这里都可以适当的拓展,比如加条件,重命名等等。
3、使用sum求和:select sum(字段名) from tablename;


4、使用max和min求最大值、最小值:


5、注意:使用这些函数时,如果数据库中没有数据,count返回0,其他的返回null;


6、使用group by对数据进行分组:select 字段名。。。 from tablename group by 字段名。。。;可以把分组、排序、统计等等都结合在一起使用,实际应用中也多是这样的:


7、使用group_concat()实现显示没个分组中的字段:


8、如果要在分组查询中加入条件,则必须使用having而不是where:


9、如果使用条件分组的同时还要排序,则order by必须位于having后边:


mysql常用基础操作语法(七)--统计函数和分组查询【命令行模式】
标签: