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;
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;
相关文章推荐
- B、B+树
- mysql联合索引
- mysql慢查询
- mysql 关键词相关度排序方法详细示例分析
- mysql 读写分离
- MYSQL性能查看(命中率,慢查询)
- mysql服务器查询慢原因分析与解决方法小结
- MYSQL开启慢查询日志实施
- mysql中文乱码解决
- 《Mysql 公司职员学习篇》 第一章 小A的烦恼
- mysql索引总结----mysql 索引类型以及创建
- 配置mysql5.5主从服务器
- Ubuntu下的MySQL安装
- MySQL数据库查询操作
- Mysql视图使用总结
- MySQL数据库增、删、改操作
- mysql数据库分页查询
- 不停止MySQL服务增加从库的两种方式
- mysql下载于部署
- mysql5.6新特性总结