MySQL的基本函数

charset(str) //返回字串字符集

mysql> select charset('demacia');
+--------------------+
| charset('demacia') |
+--------------------+
| utf8               |
+--------------------+
1 row in set

concat(string2 [,... ]) //连接字串

 

mysql> select concat('陷阵之志','有死无生');
+-------------------------+
| concat('陷阵之志','有死无生') |
+-------------------------+
| 陷阵之志有死无生              |
+-------------------------+
1 row in set

 

lcase(string2 ) //转换成小写

mysql> select lcase('DEMACIA');
+------------------+
| lcase('DEMACIA') |
+------------------+
| demacia          |
+------------------+
1 row in set

left(string2 ,length ) //string2中的左边起取length个字符

 

mysql> select left('demacia',3);
+-------------------+
| left('demacia',3) |
+-------------------+
| dem               |
+-------------------+
1 row in set

 

length(string ) //string长度

 

+-------------------+
| length('demacia') |
+-------------------+
|                 7 |
+-------------------+
1 row in set

 

load_file(file_name ) //从文件读取内容

instr (string ,substring ) //返回substring首次在string中出现的位置,不存在返回0

 

mysql>  select 
instr('demacia','a');
+----------------------+
| instr('demacia','a') |
+----------------------+
|                    4 |
+----------------------+
1 row in set

 

locate(substring , string [,start_position ] ) INSTR,但可指定开始位置

mysql> select 
locate('demacia',1,4);
+-----------------------+
| locate('demacia',1,4) |
+-----------------------+
|                     0 |
+-----------------------+
1 row in set

substring(str , position [,length ]) //从str的position开始,取length个字符,

 

mysql> select substring('demacia',1,3);
+--------------------------+
| substring('demacia',1,3) |
+--------------------------+
| dem                      |
+--------------------------+
1 row in set

 

lpad(string2 ,length ,pad ) //重复用pad加在string开头,直到字串长度为length

rpad (string2 ,length ,pad) //在str后用pad补充,直到长度为length

LTRIM (string2 ) //去除前端空格

RTRIM (string2 ) //去除后端空格
repeat(string2 ,count ) //重复count

mysql> select repeat('demacia',3);
+-----------------------+
| repeat('demacia',3)   |
+-----------------------+
| demaciademaciademacia |
+-----------------------+
1 row in set

replace(str ,search_str ,replace_str ) //str中用replace_str替换search_str

strcmp(string1 ,string2 ) //逐字符比较两字串大小,

 

mysql> select strcmp('demacia','wansui');
+----------------------------+
| strcmp('demacia','wansui') |
+----------------------------+
| -1 |
+----------------------------+
1 row in set

注:mysql中处理字符串时,默认第一个字符下标为1,即参数position必须大于等于1 

(2).数学类

ABS (number2 ) //绝对值

mysql> select abs(-1);
+---------+
| abs(-1) |
+---------+
|       1 |
+---------+
1 row in set

BIN (decimal_number ) //十进制转二进制

 

mysql> select bin(255);
+----------+
| bin(255) |
+----------+
| 11111111 |
+----------+
1 row in set

 

ceiling(number2 ) //向上取整

mysql> select ceiling(-12.5);
+----------------+
| ceiling(-12.5) |
+----------------+
|            -12 |
+----------------+
1 row in set

floor(number2 ) //向下取整

 

mysql> select floor
(-12.5);
+--------------+
| floor(-12.5) |
+--------------+
|          -13 |
+--------------+
1 row in set

 

round (number [,decimals ]) //四舍五入,decimals为小数位数]

 

mysql> select round(3.654,1);
+----------------+
| round(3.654,1) |
+----------------+
| 3.7            |
+----------------+
1 row in set

 

conv(number2,from_base,to_base) //进制转换

 

mysql> select conv(255,10,2);
+----------------+
| conv(255,10,2) |
+----------------+
| 11111111       |
+----------------+
1 row in set

format(number,decimal_places ) //保留小数位数

mysql> select format(3.141592653,2);
+-----------------------+
| format(3.141592653,2) |
+-----------------------+
| 3.14                  |
+-----------------------+
1 row in set

hex(DecimalNumber ) //转十六进制

 

mysql> select hex(255);
+----------+
| hex(255) |
+----------+
| FF       |
+----------+
1 row in set

 

注:HEX()中可传入字符串,则返回其ASC-11码,如HEX('DEF')返回

444546     

 

 

mysql> select hex('DEF');
+------------+
| hex('DEF') |
+------------+
| 444546     |
+------------+
1 row in set

 

也可以传入十进制整数,返回其十六进制编码,如HEX(25)返回19

least(number , number2 [,..]) //求最小值

mysql> select least(1,3,5,-1,-2);
+--------------------+
| least(1,3,5,-1,-2) |
+--------------------+
|                 -2 |
+--------------------+
1 row in set

mod (numerator ,denominator ) //求余

 

mysql> select mod(5,2);
+----------+
| mod(5,2) |
+----------+
|        1 |
+----------+
1 row in set

 

power (number ,power ) //求指数

 

mysql> select power(2,7);
+------------+
| power(2,7) |
+------------+
|        128 |
+------------+
1 row in set

 

rand([seed]) //随机数

(3).日期时间类

ADDTIME (date2 ,time_interval ) //将time_interval加到date2
CONVERT_TZ (datetime2 ,fromTZ ,toTZ ) //转换时区
CURRENT_DATE ( ) //当前日期

mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2016-06-28     |
+---------------

CURRENT_TIME ( ) //当前时间

mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 17:04:10       |
+----------------+
1 row in set

CURRENT_TIMESTAMP ( ) //当前时间戳

mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2016-06-28 17:04:34 |
+---------------------+
1 row in set

DATE (datetime ) //返回datetime的日期部分

mysql> select date(current_timestamp);
+-------------------------+
| date(current_timestamp) |
+-------------------------+
| 2016-06-28              |
+-------------------------+
1 row in set

DATE_ADD (date2 , INTERVAL d_value d_type ) //在date2中加上日期或时间

DATE_FORMAT (datetime ,FormatCodes ) //使用formatcodes格式显示datetime

DATE_SUB (date2 , INTERVAL d_value d_type ) //在date2上减去一个时间
DATEDIFF (date1 ,date2 ) //两个日期差
DAY (date ) //返回日期的天
DAYNAME (date ) //英文星期
DAYOFWEEK (date ) //星期(1-7) ,1为星期天
DAYOFYEAR (date ) //一年中的第几天
EXTRACT (interval_name FROM date ) //从date中提取日期的指定部分
MAKEDATE (year ,day ) //给出年及年中的第几天,生成日期串
MAKETIME (hour ,minute ,second ) //生成时间串
MONTHNAME (date ) //英文月份名
NOW ( ) //当前时间
SEC_TO_TIME (seconds ) //秒数转成时间
STR_TO_DATE (string ,format ) //字串转成时间,以format格式显示
TIMEDIFF (datetime1 ,datetime2 ) //两个时间差
TIME_TO_SEC (time ) //时间转秒数]
WEEK (date_time [,start_of_week ]) //第几周
YEAR (datetime ) //年份
DAYOFMONTH(datetime) //月的第几天
HOUR(datetime) //小时
LAST_DAY(date) //date的月的最后日期
MICROSECOND(datetime) //微秒
MONTH(datetime) //月
MINUTE(datetime) //分返回符号,正负或0
SQRT(number2) //
开平方

 

posted @ 2016-06-28 17:11  公众号java-codestack  阅读(368)  评论(0编辑  收藏  举报