mysql函数和分组聚合

使用limit限定输出个数

select * from 表名 limit 个数;
select * from 表名 limit 起始数,个数;

mysql的常用函数

  1. concat函数 可以将查询出的数据连接成一个
select concat(字段名,字段名) from 表名;
select concat(字段名,'-',字段名) from 表名;
  1. rand函数 随机数
select * from 表名 order by rand();
//随机取限定个数
select * from 表名 order by rand() limit 个数;

3.count函数 统计个数

//统计数据总共有多少行
select count(*) from 表名;
  • sum()函数 求和
select sum(字段名) from 表名;
  • avg() 平均数
select avg(字段名) from 表名;
  • min()函数 最小值
select min(字段名) from 表名;
  • max()函数 最大值
select max(字段名) from 表名;

分组聚合

//分组
select * from 表名 grounp by 字段名;
//集合
select 字段名1,count(字段名)  from 表名 grounp by 字段名;
//例如 每个班级有多少人数
select contat(class_id,'-','class') class,count(id) student_count from user group by class_id;

你可能感兴趣的:(mysql函数和分组聚合)