您的位置:首页 > 数据库 > MySQL

MySQL常用函数

2016-03-20 20:33 405 查看
1、concat()//连接函数

select concat(id,age) from user;    //将id,name两列的数据连接起来

select concat("aaa","bbb","ccc");  //变成aaabbbccc

2、rand()函数,随机排序

select*from user order by rand() limit 1;

3、count()   //统计个数

count(*);快速得到行数

select count(id) as total from user where name='user4'; //可以得到user4的发帖数

4、sum()   //求和

select sum(id) as '总和' from user where name="user4";

5、avg()   //求平均数

6、max()   //求最大值

7、min()   //求最小值

8、group by 分组聚合函数

只分组没有意义,同时应该有聚合:例如count()、sum()、avg()、max()、min()

select name,count(id)
as total from user group by name order by total;

如果同时有group by和order by,group by 必须在order by 前面

9、having

如果分组之后的结果中需要继续加条件,不能再用where,只能用having对分组后的结果进行筛选

select name,count(id) as total from user group by name
having total>5 order by total;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: