首页  :: 新随笔  :: 管理

MySQL常用函数

Posted on 2021-12-09 12:13  高&玉  阅读(48)  评论(0编辑  收藏  举报

日期时间类

查看日期时间

current_date()

mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2021-12-09     |
+----------------+

 

current_time()

now()

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2021-12-09 14:53:13 |
+---------------------+

 

now()

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2021-12-09 14:53:13 |
+---------------------+

 

current_timestamp()

mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2021-12-09 14:56:47 |
+---------------------+

日期时间格式转换

语法:

  • date_format(datetime,formatting)  #可以格式日期和时间(例如 YYYY-MM-DD HH:MM:SS)和(HH:MM:SS) 
  • time_format(datetime,formatting)  #只可以格式时间(HH:MM:SS) 

 

format字符串中常用的说明符:

说明符

说明

%a

工作日的缩写名称  (Sun..Sat)

%b

月份的缩写名称  (Jan..Dec)

%c

月份,数字形式(0..12)

%D

带有英语后缀的该月日期  (0th, 1st, 2nd, 3rd, ...)

%d

该月日期, 数字形式 (00..31)

%e

该月日期, 数字形式(0..31)

%f

微秒 (000000..999999)

%H

小时(00..23)

%h

小时(01..12)

%I

小时 (01..12)

%i

分钟,数字形式 (00..59)

%j

一年中的天数 (001..366)

%k

小时 (0..23)

%l

小时 (1..12)

%M

月份名称 (January..December)

%m

月份, 数字形式 (00..12)

%p

上午(AM)或下午( PM)

%r

时间 , 12小时制 (小时hh:分钟mm:秒数ss 后加 AM或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位数)

%%

‘%’文字字符

 

date_format()

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2021-12-09 15:58:00 |
+---------------------+

mysql> select date_format(now(),'%Y-%m-%d');
+-------------------------------+
| date_format(now(),'%Y-%m-%d') |
+-------------------------------+
| 2021-12-09                    |
+-------------------------------+

 

time_format()

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2021-12-09 15:57:35 |
+---------------------+

mysql> select time_format(now(),'%H');
+-------------------------+
| time_format(now(),'%H') |
+-------------------------+
| 15                      |
+-------------------------+

字符串类型

format_statement()

format_statement() 默认截取的最大长度为64个字符(可以通过@sys.statement_truncate_len用户变量修改)。

mysql> select sys.format_statement('A collection of views, procedures and functions, designed to make readingraw Performance Schema data easier.');
+--------------------------------------------------------------------------------------------------------------------------------------+
| sys.format_statement('A collection of views, procedures and functions, designed to make readingraw Performance Schema data easier.') |
+--------------------------------------------------------------------------------------------------------------------------------------+
| A collection of views, procedu ... erformance Schema data easier.                                                                    |
+--------------------------------------------------------------------------------------------------------------------------------------+

mysql> select length(sys.format_statement('A collection of views, procedures and functions, designed to make readingraw Performance Schema data easier.'));
+----------------------------------------------------------------------------------------------------------------------------------------------+
| length(sys.format_statement('A collection of views, procedures and functions, designed to make readingraw Performance Schema data easier.')) |
+----------------------------------------------------------------------------------------------------------------------------------------------+
|                                                                                                                                           65 |
+----------------------------------------------------------------------------------------------------------------------------------------------+

mysql> select length(sys.format_statement('hello'));
+---------------------------------------+
| length(sys.format_statement('hello')) |
+---------------------------------------+
|                                     5 |
+---------------------------------------+

 

 

 

数字类型

format_bytes()

从MySQL 8.0.16开始,format_bytes()不推荐使用,在将来的MySQL版本中将删除。使用它的应用程序应该迁移为使用内置 FORMAT_BYTES()功能。

给定一个字节数,将其转换为人类可读的格式,并返回一个由值和单位指示符组成的字符串。根据值的大小,单位部分为 bytes,KiB(千字节), MiB(兆字节),GiB (千兆字节),TiB(兆字节)或 PiB(千兆字节)。

mysql> select FORMAT_BYTES(3165764429);
+--------------------------+
| FORMAT_BYTES(3165764429) |
+--------------------------+
| 2.95 GiB                 |
+--------------------------+

 

 

 

 

 

 

 

 

 

 

 

 

 

 

OK