博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MySQL函数的使用
阅读量:6946 次
发布时间:2019-06-27

本文共 10919 字,大约阅读时间需要 36 分钟。

以下列出mysql函数的使用,并不完全,涉及到多少写多少。

 

length(str):返回字符串(str)的字符长度。一个汉字算三个字符,一个数字或字母算一个字符。

select length('测试');        -- 6select length('123abc');    -- 6

char_length(str):返回字符串(str)的字符长度。一个汉字、数字或字母都算一个字符。

select char_length('测试');        -- 2select char_length('123abc');    -- 6

instr(str,substr):返回指定字符串(substr)在字符串(str)中的第一次出现的位置。如果未找到则返回0。

select instr('football','f');    -- 1select instr('football','o');    -- 2select instr('football','ba');    -- 5

locate(substr,str):返回指定字符串(substr)在字符串(str)中的第一次出现的位置。如果未找到则返回0。

select locate('ba','football');        -- 5select locate('o','football');        -- 2

locate(substr,str,pos):返回指定字符串(substr)在字符串(str)中的第(pos)位之后第一次出现的位置。如果未找到则返回0。

select locate('o','football',3);    -- 3select locate('o','football',4);    -- 0

concat(str1,str2,...):返回所有参数拼接起来产生的字符串。如果有任何一个参数位null,则返回null。

select concat('w','h','at');        -- whatselect concat('w','h','at',null);    -- null

concat_ws(separator,str1,str2,...):返回所有参数拼接起来产生的字符串,第一个参数作为后面参数拼接的分隔符。如果分隔符为null,则返回null,除分隔符外的其他参数为null,则忽略。

select concat_ws(',','first','second','third');    -- first,second,thirdselect concat_ws(';','11','22','33',null);        -- 11;22;33select concat_ws(null,'11','22','33',null);        -- null

left(str,length):从字符串(str)左起第一个字符开始,返回指定长度(length)的子字符串。

select left('mysql',2);        -- my

right(str,length):从字符串(str)右起第一个字符开始,返回指定长度(length)的子字符串。

select right('mysql',3);    -- sql

substring(str,pos):返回字符串(str)从第(pos)个字符开始之后的所有字符组成的子字符串。pos为正数,从左起;pos为负数,从右起。

select substring('everyone',3);        -- eryoneselect substring('everyone',-3);    -- one

substring(str,pos,length):返回字符串(str)从第(pos)个字符开始,之后指定长度(length)的子字符串。pos为正数,从左起;pos为负数,从右起。

select substring('everyone',1,5);    -- everyselect substring('everyone',-3,3);    -- one

substring_index(str,delim,count):返回从字符串(str)第一个字符开始,到字符串中第(count)次出现的分隔符(delim)之间的子字符串。count为正数,从左起;count为负数,从右起。

如果在字符串(str)中未找到分隔符(delim)的位置,或者未找到指定次数(count)出现的分隔符的位置时,则返回整个字符串。分隔符(delim)不一定为符号,也可以为其它自定义字符。

select substring_index('11;22;33;',';',2);    -- 11;22select substring_index('11;22;33;',',',2);    -- 11;22;33;select substring_index('11;22;33;',';',-2);    -- 33;

convert(value,type):类型或格式转换。可转换的类型是有限制的,可以是以下类型中之一:binary、char(n)、date、time、datetime、decimal、signed、unsigned。

-- 数字转换为字符串select convert(33,char);    -- 33(字符串类型)-- 字符串转换为数字select convert('333',signed);    -- 333(数字值类型)-- 把字符串编码格式转换为Unicode编码格式select convert('一' using ucs2);-- 把字符串编码格式转换为UTF8编码格式select convert('abc' using utf8);-- 把字符串编码格式转换为GBK编码格式select convert('一' using gbk);-- 把字符串编码格式转换为GB2312编码格式select convert('一' using gb2312);

cast(value as type):类型转换。可转换的类型是有限制的,可以是以下类型中之一:binary、char(n)、date、time、datetime、decimal、signed、unsigned。

-- 数字转换为字符串select cast(333 as char);-- 字符串转换为数字select cast('3333' as signed);

hex(str):把指定的字符串(str)转换为16进制值。

-- 把字符串转换为16进制值select hex(convert('一' using ucs2));    -- 4E00

unhex(str):把指定的16进制字符串(str)转换为字符串。

-- 把16进制值转换为字符串select convert(unhex('4E00') using ucs2);    -- 一

ord(str):把指定的字符串(str)转换为ASCII值。

-- 把字符串转换为ASCII值select ord(convert('A' using ucs2));    -- 65select ord(convert('一' using ucs2));    -- 19968

ascii(str):把指定的字符串(str)转换为ASCII值。

-- 把字符串转换为ASCII值select ascii(convert('A' using utf8));    -- 65

now():获取当前的日期与时间,即“YYYY-MM-dd HH:mm:ss”格式。

-- 获取当前日期时间select now();    -- 2018-11-16 15:11:26

current_timestamp():获取当前的时间戳,即“YYYY-MM-dd HH:mm:ss”格式。

-- 获取当前的时间戳select current_timestamp;    -- 2018-11-16 15:11:26select current_timestamp();    -- 2018-11-16 15:11:26

curdate():获取当前的日期,即“YYYY-MM-dd”格式。

-- 获取当前的日期select curdate();    -- 2018-11-16

current_date():获取当前的日期,即“YYYY-MM-dd”格式。

-- 获取当前的日期select current_date();        -- 2018-11-16

curtime():获取当前的时间,即“HH:mm:ss”格式。

-- 获取当前的时间select curtime();    -- 15:11:26

current_time():获取当前的时间,即“HH:mm:ss”格式。

-- 获取当前的时间select current_time();    -- 15:11:26

last_day(datetime):获取指定的日期时间中月份的最后一天,可以借此获取指定的月份有多少天。

-- 获取指定的日期时间中月份的最后一天select last_day('2018-10-10');    -- 2018-10-31select last_day('2018-10-10 12:30:30');    -- 2018-10-31select day(last_day('2018-10-10'));        -- 31

dayofweek(datetime):获取指定的日期时间是星期几。(1表示星期日,2表示星期一。。。7表示星期六,ODBC标准)

-- 获取指定的日期时间是星期几select dayofweek('2018-10-10');    -- 4select dayofweek('2018-10-10 12:30:30');    -- 4

weekday(datetime):获取指定的日期时间是星期几。(0表示星期一,1表示星期二。。。6表示星期日)

-- 获取指定的日期时间是星期几select weekday('2018-10-10');    -- 2select weekday('2018-10-10 12:30:30');    -- 2

dayofmonth(datetime):获取指定的日期时间中是该月的第几天。(返回值范围为1至31)

-- 获取指定的日期时间中是该月的第几天select dayofmonth('2018-10-10');    -- 10select dayofmonth('2018-10-10 12:30:30');    -- 10

dayofyear(datetime):获取指定的日期时间中是该年的第几天。

-- 获取指定的日期时间中是该年的第几天select dayofyear('2018-10-10');    -- 283select dayofyear('2018-10-10 12:30:30');    -- 283

dayname(datetime):获取指定的日期时间是星期几的英文。

-- 获取指定的日期时间是星期几的英文select dayname('2018-10-10');    -- Wednesdayselect dayname('2018-10-10 12:30:30');    -- Wednesday

monthname(datetime):获取指定的日期时间中月份的英文。

-- 获取指定的日期时间中月份的英文select monthname('2018-10-10');    -- Octoberselect monthname('2018-10-10 12:30:30');    -- October

quarter(datetime):获取指定的日期时间是当年的第几季度。(1至3月表示第一季度。。。9-12月表示第四季度)

-- 获取指定的日期时间是当年的第几季度select quarter('2018-10-10');    -- 4select quarter('2018-10-10 12:30:30');    -- 4

sec_to_time(second):把秒数转换为时间。

-- 把秒数转换为时间select sec_to_time(120);    -- 00:02:00select sec_to_time(3600);    -- 01:00:00

time_to_sec(time):把时间转换为秒数。

-- 把时间转换为秒数select time_to_sec('00:02:00');    -- 120select time_to_sec('01:00:00');    -- 3600

date_format(datetime,format):获取指定格式的日期时间。

-- 获取指定格式的日期时间-- 格式    描述-- %a    缩写星期名(Sun、Sat等)-- %b    缩写月名(Jan、Dec等)-- %c    月,数值(1-12)-- %D    带有英文前缀的月中的天(1th、3th、10th等)-- %d    月的天,数值(00-31)-- %e    月的天,数值(0-31)-- %f    微秒-- %H    小时 (00-23)-- %h    小时 (01-12)-- %I    小时 (01-12)-- %i    分钟,数值(00-59)-- %j    年的天 (001-366)-- %k    小时 (0-23)-- %l    小时 (1-12)-- %M    月名(January、October等)-- %m    月,数值(01-12)-- %p    AM 或 PM-- %r    时间,12-小时(hh:mm:ss AM 或hh:mm:ss PM)-- %S    秒(00-59)-- %s    秒(00-59)-- %T    时间, 24-小时 (hh:mm:ss)-- %U    周 (00-53) 星期日是一周的第一天-- %u    周 (00-53) 星期一是一周的第一天-- %V    周 (01-53) 星期日是一周的第一天,与 %X 使用-- %v    周 (01-53) 星期一是一周的第一天,与 %x 使用-- %W    星期名-- %w    周的天 (0=星期日, 6=星期六)-- %X    年,其中的星期日是周的第一天,4 位,与 %V 使用-- %x    年,其中的星期一是周的第一天,4 位,与 %v 使用-- %Y    年,4 位-- %y    年,2 位select date_format('2018-10-10','%Y-%m-%d %H:%i:%s');    -- 2018-10-10 00:00:00select date_format('2018-10-10 12:30:30','%Y-%m-%d');    -- 2018-10-10

time_format(datetime,format):获取指定格式的时间。该方法与date_format()方法类似,但time_format()方法只处理时间部分。

-- 获取指定格式的时间。select time_format('12:30:30','%H:%i:%s');    -- 12:30:30select time_format('12:30:30','%r');    -- 12:30:30 PM

str_to_date(datetime,format):将指定的字符串转换为日期时间的格式,即转换为“YYYY-MM-dd HH:mm:ss”的格式。

-- 将指定的字符串转换为日期时间的格式select str_to_date('10/25/2018','%m/%d/%Y');    -- 2018-10-25select str_to_date('10.25.2018 12.30.30','%m.%d.%Y %H.%i.%s');    -- 2018-10-25 12:30:30

makedate(year,dayofyear):获取根据多个值拼成的日期。第一个参数表示年份,第二个参数表示指定该年的第多少天。

-- 获取根据多个值拼成的日期select makedate(2018,180);    -- 2018-06-29select makedate(2018,15);    -- 2018-01-15

maketime(hour,minute,second):获取根据多个值拼成的时间。

-- 获取根据多个值拼成的时间select maketime(12,20,30);    -- 12:20:30select maketime(23,59,59);    -- 23:59:59

timestamp(datetime_exp1[,datetime_exp2]):获取指定日期时间的时间戳,以及两个日期时间的相加运算。

-- 获取指定日期时间的时间戳select timestamp('2018-10-10');    -- 2018-10-10 00:00:00select timestamp('2018-10-10 12:30:30');    -- 2018-10-10 12:30:30-- 获取两个指定日期时间的参数的和的时间戳,即第一个日期时间加上第二个时间的和select timestamp('2018-10-10 12:30:30','01:01:01');    -- 2018-10-10 13:31:31-- 最多只能指定到日,不能指定月和年select timestamp('2018-10-10 12:30:30','01 01:01:01');    -- 2018-10-11 13:31:31

timestampadd(unit,interval,datetime):在指定的年、月、日、小时、分钟、秒等单位上加上指定的值。

-- 在指定的年、月、日、小时、分钟、秒等单位上加上指定的值select timestampadd(day,1,'2018-10-10 12:30:30');        -- 2018-10-11 12:30:30select timestampadd(month,1,'2018-10-10 12:30:30');        -- 2018-11-10 12:30:30

timestampdiff(unit,datetime_exp1,datetime_exp2):比较两个日期时间相差的指定的年、月、日、小时、分钟、秒等单位的值。

-- 比较两个日期时间相差的指定的年、月、日、小时、分钟、秒等单位的值select timestampdiff(day,'2018-10-11 12:30:30','2018-11-10 12:30:30');        -- 30select timestampdiff(hour,'2018-10-11 12:30:30','2018-11-10 12:30:30');        -- 720

datediff(datetime_exp1,datetime_exp2):比较两个日期时间中,第一个日期时间相对于第二个日期时间相差的天数。

-- 比较两个日期时间中,第一个日期时间相对于第二个日期时间相差的天数select datediff('2018-10-11','2018-11-10');        -- -30select datediff('2018-12-20 12:00:00','2018-10-25 12:30:30');        -- 56

timediff(time_exp1,time_exp2):比较两个时间中,第一个时间相对于第二个时间相差的时:分:秒。

-- 比较两个时间中,第一个时间相对于第二个时间相差的时:分:秒select timediff('09:00:00','12:30:30');        -- -03:30:30select timediff('12:00:00','08:30:30');        -- 03:29:30

date_add(datetime,interval value unit):在指定的日期时间上增加指定的年、月、日、小时、分钟、秒等单位的值。

-- 在指定的日期时间上增加指定的年、月、日、小时、分钟、秒等单位的值select date_add('2018-10-10',interval 1 day);            -- 2018-10-11select date_add('2018-10-10 12:30:30',interval 3 month);        -- 2019-01-10 12:30:30select date_add('2018-10-10 12:30:30',interval -10 minute);        -- 2018-10-10 12:20:30

date_sub(datetime,interval value unit):在指定的日期时间上减少指定的年、月、日、小时、分钟、秒等单位的值。

-- 在指定的日期时间上减少指定的年、月、日、小时、分钟、秒等单位的值select date_sub('2018-10-10',interval 1 day);            -- 2018-10-09select date_sub('2018-10-10 12:30:30',interval 3 month);        -- 2018-07-10 12:30:30select date_sub('2018-10-10 12:30:30',interval -10 minute);        -- 2018-10-10 12:40:30

 获取指定的日期时间中指定的年、月、日、小时、分钟、秒等单位的值。

set @dt='2018-10-10 12:30:30.123456';select date(@dt);         -- 2018-10-10select time(@dt);         -- 12:30:30.123456select year(@dt);         -- 2018select quarter(@dt);     -- 4select month(@dt);         -- 10select week(@dt);         -- 40select day(@dt);         -- 10select hour(@dt);         -- 12select minute(@dt);     -- 30select second(@dt);     -- 30select microsecond(@dt); -- 123456select yearweek(@dt);      -- 201840

extract(unit from datetime):获取指定的日期时间中指定的年、月、日、小时、分钟、秒等单位的值。

set @dt='2018-10-10 12:30:30.123456';select extract(year from @dt);             -- 2018select extract(quarter from @dt);         -- 4select extract(month from @dt);         -- 10select extract(week from @dt);             -- 40select extract(day from @dt);             -- 10select extract(hour from @dt);             -- 12select extract(minute from @dt);         -- 30select extract(second from @dt);         -- 30select extract(microsecond from @dt);     -- 123456select extract(year_month from @dt);             -- 201810select extract(day_hour from @dt);                 -- 1012select extract(day_minute from @dt);             -- 101230select extract(day_second from @dt);             -- 10123030select extract(day_microsecond from @dt);         -- 10123030123456select extract(hour_minute from @dt);             -- 1230select extract(hour_second from @dt);             -- 123030select extract(hour_microsecond from @dt);         -- 123030123456select extract(minute_second from @dt);         -- 3030select extract(minute_microsecond from @dt);     -- 3030123456select extract(second_microsecond from @dt);     -- 30123456

 

转载于:https://www.cnblogs.com/Brambling/p/9259631.html

你可能感兴趣的文章
Win10下安装Ubuntu16.04虚拟机并搭建TensorFlow1.3环境
查看>>
leetcode 108. Convert Sorted Array to Binary Search Tree
查看>>
【商城购物车】购物车逻辑
查看>>
PCIE协议解析 synopsys IP loopback 读书笔记(1)
查看>>
创建maven工程的时候卡死的解决办法
查看>>
Eclipse将引用了第三方jar包的Java项目打包成jar文件的两种方法
查看>>
微信小程序保存图片功能实现
查看>>
【Shiro】小读Shiro Filter(未完待续)
查看>>
Android环信即时通讯集成坑爹 注册报错208解决
查看>>
Flink及主流流框架spark,storm比较
查看>>
mysql按位的索引判断位的值
查看>>
一套简约漂亮的响应式博客园主题皮肤
查看>>
js获取时间戳
查看>>
Java数据结构和算法(四):栈
查看>>
为什么我的mac插入耳机耳机没有声音呢?
查看>>
ArcGIS js api 手动构建FeatureLayer
查看>>
Spark RDD持久化、广播变量和累加器
查看>>
Exception in thread "main" javax.validation.ValidationException: Unable to find a default provider
查看>>
Step by step SQL Server 2012的安装
查看>>
使用using 语句
查看>>