- 浏览: 238696 次
最新评论
一、字符串类
1、left(str, length) 从左开始截取字符串 /right(str, length) 从右开始
说明:left(被截取字段,截取长度)
例:select left(title,5) from articles;
select right("abcdefghi",3); ---ghi
2、ltrim(str) 去掉左侧的空格 /rtrim 去掉右侧的空格 /trim 去掉两侧的空格
例:select ltrim(" abc de fghi "); ---“abc de fghi ”
select rtrim(" abc de fghi "); ---“ abc de fghi”
select trim(" abc de fghi "); ---“abc de fghi”
3、substring(str, pos, [length]) 截取字符串
说明:substring(被截取字段,从第几位开始截取)
例:select substring(title,5) from articles; ---从第5个字符开始取后面的所有
select substring(title,5,10) from articles; ---从第5个字符开始截取10个字符
select substring(title,-2) from articles; ---从倒数第2个字符开始截取到最后
4、substring_index(str,delim,count)按关键字截取字符串
说明:substring_index(被截取字段,关键字,关键字出现的次数)
例:select substring_index ("www.baidu.com",".",2); ---www.baidu
select substring_index(`msg`,"/",5) from update_log where action=1;
(注:如果关键字出现的次数是负数 如-2 则是从后倒数,到字符串结束)
5、concat(str1,str2....strn) 字符串连接
例:select concat ("www.baidu.com",".","cn"); ---www.baidu.com.cn
select concat (title,"-",body) from articles;
6、concat_ws(sep,str1,str2.....strn) 字符串连接,并用sep分隔开
例:select concat_ws (";","abc","def"); ---abc;def
7、insert(str1,x,y,str2) 将字符串str1从第x位置开始,y个字符长的子串替换为字符串str2,返回结果
例:select insert (title,2,3,"aaa") from articles; ---MaaaL
8、lower(str) 将字符串全部换成小写 /upper(str) 将字符串全部换成大写
例:select lower("abCDefG"); ---abcdefg
select upper("abCDefG") ---ABCDEFG
9、length(str) 字符串的长度
例:select length("abCDefG"); --7
10、position(substr in str) 返回子串substr在字符串str中第一次出现的位置
例:select position("C" in "abCDefG"); --3
二、数字类
1、abs(x) 返回x的绝对值
例:select abs(-123); ---123
2、ceiling(x) 返回大于x的最小整数值
例:select ceiling(-123.456); -123
select ceiling(123.456); 124
3、floor(x) 返回小于x的最大整数值
例:select floor(123.456); 123
select floor(-123.456); -124
4、greatest(x1,x2,...,xn) 返回集合中最大的值 /least(x1,x2,...,xn) 返回集合中最小的值
例:select greatest(1,10,100,-5); 100
select least(1,10,100,-5) -5
5、mod(x,y) 返回x/y的模(余数)
例:select mod(100,3); 1
6、rand() 返回0到1内的随机值
例:select rand(); 0.3541006747205748
7、round(x,y) 返回参数x的四舍五入的有y位小数的值
例:select round(123.456,2); 123.46
8、sqrt(x) 返回一个数的平方根
例:select sqrt(144); 12
三、日期时间类
1、curdate()或current_date() 返回当前的日期
例:select curdate(); 2015-02-04
2、curtime()或current_time() 返回当前的时间 now() 当前的日期和时间
例:select curtime(); 17:15:22
select now(); 2015-02-04 17:16:01
3、date_add(date,interval int keyword) 返回日期date加上间隔时间int的结果(int必须按照关键字进行格式化)
例:select date_add(current_date,interval 6 month); 2015-08-04
4、date_format(date,fmt) 依照指定的fmt格式格式化日期date值
例: select date_format(now(),'%m-%d-%Y %H:%i:%s %a') 02-04-2015 17:21:41 Wed
5、date_sub(date,interval int keyword) 返回日期date加上间隔时间int的结果(int必须按照关键字进行格式化)
例:select date_sub(current_date,interval 6 month);
6、dayofweek(date) 返回date所代表的一星期中的第几天(1~7)
例:select dayofweek(now()); 4 今天是星期三
注意:1=星期天,2=星期一, ……7=星期六
7、dayofmonth(date) 返回date是一个月的第几天(1~31) /dayofyear(date) 返回date是一年的第几天(1~366)
例:select dayofmonth(now()); 4
select dayofyear(now()); 35
8、dayname(date) 返回date的星期名 /month(date) 返回date的月份名
例: select dayname(current_date); Wednesday
select monthname(current_date); February
9、hour(time) 返回time的小时值(0~23)
/minute(time) 返回time的分钟值(0~59)
/month(date) 返回date的月份值(1~12)
/quarter(date) 返回date在一年中的季度(1~4)
/week(date) 返回日期date为一年中第几周(0~53)
/year(date) 返回日期date的年份(1000~9999)
例:select hour(current_time); 17
select quarter(current_date); 1
10、extract(unit FROM date) 返回日期/时间的单独部分,比如年、月、日、小时、分钟等等。
unit 常用的有:microsecond second minute day week month quarter year day_minute hour_minute year_month
例:select extract(year_month from now()); 201502
select extract(minute from now()); 41
四、其他
1、cast(value as type) 类型转换函数 /convert(value,type)
说明:它可以把一个值转化为指定的数据类型。类型有
二进制,同带binary前缀的效果 : binary
字符型,可带参数 : char()
日期 : date
时间: time
日期时间型 : datetime
浮点数 : decimal
整数 : signed
无符号整数 : unsigned
例:select cast('3.35' as signed); 3
2、系统信息函数
database() 返回当前数据库名
benchmark(count,expr) 将表达式expr重复运行count次
connection_id() 返回当前客户的连接ID
found_rows() 返回最后一个select查询进行检索的总行数
user()或system_user() 返回当前登陆用户名
version() 返回MySQL服务器的版本
1、left(str, length) 从左开始截取字符串 /right(str, length) 从右开始
说明:left(被截取字段,截取长度)
例:select left(title,5) from articles;
select right("abcdefghi",3); ---ghi
2、ltrim(str) 去掉左侧的空格 /rtrim 去掉右侧的空格 /trim 去掉两侧的空格
例:select ltrim(" abc de fghi "); ---“abc de fghi ”
select rtrim(" abc de fghi "); ---“ abc de fghi”
select trim(" abc de fghi "); ---“abc de fghi”
3、substring(str, pos, [length]) 截取字符串
说明:substring(被截取字段,从第几位开始截取)
例:select substring(title,5) from articles; ---从第5个字符开始取后面的所有
select substring(title,5,10) from articles; ---从第5个字符开始截取10个字符
select substring(title,-2) from articles; ---从倒数第2个字符开始截取到最后
4、substring_index(str,delim,count)按关键字截取字符串
说明:substring_index(被截取字段,关键字,关键字出现的次数)
例:select substring_index ("www.baidu.com",".",2); ---www.baidu
select substring_index(`msg`,"/",5) from update_log where action=1;
(注:如果关键字出现的次数是负数 如-2 则是从后倒数,到字符串结束)
5、concat(str1,str2....strn) 字符串连接
例:select concat ("www.baidu.com",".","cn"); ---www.baidu.com.cn
select concat (title,"-",body) from articles;
6、concat_ws(sep,str1,str2.....strn) 字符串连接,并用sep分隔开
例:select concat_ws (";","abc","def"); ---abc;def
7、insert(str1,x,y,str2) 将字符串str1从第x位置开始,y个字符长的子串替换为字符串str2,返回结果
例:select insert (title,2,3,"aaa") from articles; ---MaaaL
8、lower(str) 将字符串全部换成小写 /upper(str) 将字符串全部换成大写
例:select lower("abCDefG"); ---abcdefg
select upper("abCDefG") ---ABCDEFG
9、length(str) 字符串的长度
例:select length("abCDefG"); --7
10、position(substr in str) 返回子串substr在字符串str中第一次出现的位置
例:select position("C" in "abCDefG"); --3
二、数字类
1、abs(x) 返回x的绝对值
例:select abs(-123); ---123
2、ceiling(x) 返回大于x的最小整数值
例:select ceiling(-123.456); -123
select ceiling(123.456); 124
3、floor(x) 返回小于x的最大整数值
例:select floor(123.456); 123
select floor(-123.456); -124
4、greatest(x1,x2,...,xn) 返回集合中最大的值 /least(x1,x2,...,xn) 返回集合中最小的值
例:select greatest(1,10,100,-5); 100
select least(1,10,100,-5) -5
5、mod(x,y) 返回x/y的模(余数)
例:select mod(100,3); 1
6、rand() 返回0到1内的随机值
例:select rand(); 0.3541006747205748
7、round(x,y) 返回参数x的四舍五入的有y位小数的值
例:select round(123.456,2); 123.46
8、sqrt(x) 返回一个数的平方根
例:select sqrt(144); 12
三、日期时间类
1、curdate()或current_date() 返回当前的日期
例:select curdate(); 2015-02-04
2、curtime()或current_time() 返回当前的时间 now() 当前的日期和时间
例:select curtime(); 17:15:22
select now(); 2015-02-04 17:16:01
3、date_add(date,interval int keyword) 返回日期date加上间隔时间int的结果(int必须按照关键字进行格式化)
例:select date_add(current_date,interval 6 month); 2015-08-04
4、date_format(date,fmt) 依照指定的fmt格式格式化日期date值
例: select date_format(now(),'%m-%d-%Y %H:%i:%s %a') 02-04-2015 17:21:41 Wed
5、date_sub(date,interval int keyword) 返回日期date加上间隔时间int的结果(int必须按照关键字进行格式化)
例:select date_sub(current_date,interval 6 month);
6、dayofweek(date) 返回date所代表的一星期中的第几天(1~7)
例:select dayofweek(now()); 4 今天是星期三
注意:1=星期天,2=星期一, ……7=星期六
7、dayofmonth(date) 返回date是一个月的第几天(1~31) /dayofyear(date) 返回date是一年的第几天(1~366)
例:select dayofmonth(now()); 4
select dayofyear(now()); 35
8、dayname(date) 返回date的星期名 /month(date) 返回date的月份名
例: select dayname(current_date); Wednesday
select monthname(current_date); February
9、hour(time) 返回time的小时值(0~23)
/minute(time) 返回time的分钟值(0~59)
/month(date) 返回date的月份值(1~12)
/quarter(date) 返回date在一年中的季度(1~4)
/week(date) 返回日期date为一年中第几周(0~53)
/year(date) 返回日期date的年份(1000~9999)
例:select hour(current_time); 17
select quarter(current_date); 1
10、extract(unit FROM date) 返回日期/时间的单独部分,比如年、月、日、小时、分钟等等。
unit 常用的有:microsecond second minute day week month quarter year day_minute hour_minute year_month
例:select extract(year_month from now()); 201502
select extract(minute from now()); 41
四、其他
1、cast(value as type) 类型转换函数 /convert(value,type)
说明:它可以把一个值转化为指定的数据类型。类型有
二进制,同带binary前缀的效果 : binary
字符型,可带参数 : char()
日期 : date
时间: time
日期时间型 : datetime
浮点数 : decimal
整数 : signed
无符号整数 : unsigned
例:select cast('3.35' as signed); 3
2、系统信息函数
database() 返回当前数据库名
benchmark(count,expr) 将表达式expr重复运行count次
connection_id() 返回当前客户的连接ID
found_rows() 返回最后一个select查询进行检索的总行数
user()或system_user() 返回当前登陆用户名
version() 返回MySQL服务器的版本
发表评论
-
mysql设置外键约束on delete cascade on update cascade
2016-12-09 16:27 3666mysql设置外键约束on delet ... -
mysql权限管理(实例)
2016-05-10 17:21 1483mysql权限管理实例 本文并没有很详细的介绍对具体的对象授 ... -
mysql简单的碎片清理脚本
2016-05-10 16:52 1460mysql简单的碎片清理脚本 #!/bin/bash date ... -
mysql qpress压缩备份恢复
2016-05-03 16:30 6797说明: 1.前面博客已经介绍过gzip压缩方法,备份正常,但后 ... -
mysql xtrabackup在线搭建主从
2016-04-11 14:59 1886使用xtrabackup进行在线的主从搭建: [root@m ... -
mysql xtrabackup在线备份还原(全备+增备)
2016-04-11 14:47 1007工具安装: [root@mysqlserver var]# t ... -
mysql主库清理数据,从库保留
2016-04-01 15:26 1253因为业务需要,想在mysql主库清理一些数据,但从库想要保留, ... -
oracle,postgresql,mysql一些使用上的区别记录
2015-12-16 11:38 01.限制行数: select * from ta where ... -
数据库调优分享-mysql
2015-12-16 10:02 921数据库调优分享------参考一本mysql资料书 日常的困 ... -
mysql 安装-tina
2015-12-08 17:32 0mysql安装-tina 1、准备安装程序(http://ww ... -
mysqldump 只导入数据或只导结构
2015-12-22 10:36 2680[size=small]mysqldump只导出数据或只导出表 ... -
mysql server has gone away
2015-12-10 09:26 837mysql server has gone away,他的意思 ... -
mysql optimize 清理碎片
2015-12-09 09:26 1176---定期清理脚本 0 1 * * 4 root /root ... -
mysql binlog
2015-12-10 09:26 1320mysqld在每个二进制日志 ... -
mysql远程连接设置
2015-12-10 09:25 972远程连接mysql数据库: 连接上以后,通过这台跳转服务器远 ... -
Last_SQL_Error: Error 'Duplicate entry '1' for key 'PRIMARY''
2015-12-10 09:25 1684[size=small]-实际遇到的问题: Last_SQL ... -
[ERROR] Slave I/O: error connecting to master
2015-12-09 09:26 8067刚配置的MySQL主从,在从机上看到 点击(此处)折叠或打开 ... -
MySQL触发器简介
2015-02-05 10:33 863一、触发器基本语法 CREATE TRIGGER trigge ... -
MySQL主从切换
2015-02-05 10:32 464环境: 原主库:192.168.10.197 ---新 ... -
MySQL主从搭建
2015-02-05 10:31 753环境简介 master(主):192.168.12.101 s ...
相关推荐
基本上常用的都在这里了,如有遗漏还望指出。 MySQL常用函数大全
MySQL常用函数 MySQL常用函数
mysql常用函数 ,供大家学习 工作之用
MySQL函数举例常用函数的分类等,列举出常用函数的种类和用法,规范的举例,使您学习更轻松
Mysql常用函数列表 CONCAT (string2 [,... ]) //连接字串 INSTR (string ,substring ) //返回substring首次在string中出现的位置,不存在返回0 …… 数学类 ABS (number2 ) //绝对值 …… 日期...
MySql常用函数大全讲解,数学函数 字符串函数 日期和时间函数 条件判断函数 系统信息函数 加密函数 格式化函数
Mysql常用函数
MySQL常用函数.pdf
此文档为mysql常用函数以及概念性问题包含优化函数的使用。
mysql常用函数
mysql常用函数大全,不用每次去网上查,快速便捷
Mysql的常用函数整体, 从网上收集的一些常用函数, 进行汇总整体成文档 MySQL控制流函数: CASE WHEN[test1] THEN [result1]...ELSE [default] END如果testN是真,则返回resultN,否则返回default CASE [test] WHEN...
mysql常用函数汇总.pdf
MySQL 常用函数1. 字符串函数a. 常用函数b. 导入数据c. 示例题目2. 日期函数a. 常用函数b. 示例题目3. 数字函数a. 常用函数b. 示例题目4. 高级函数a. CASE 表达式i. 语法ii. 示例题目b. IF 表达式i. 语法ii. 示例...