了解编程的人一般都会知道函数的重要性,丰富的函数有的时候可以给我们带来事半功倍的效果,在MySQL中提供了许多的内置函数,能够帮助开发人员编写简单快捷的SQL语句,除了这些内置的函数之外,用户也可以自定义函数,本次博客整理了一下MySQL中常用的函数,简单的介绍了自定函数,MySQL版本mysql-5.7.19。

 

常用函数

1.字符串函数

字符串函数是最常用的一种函数。下表列出了常用的字符串函数:

MySQL之函数

MySQL之函数

 

2.数值函数

MySQL中另外一类就是数值函数了。这些函数可以处理很多数值方面的运算,常见的数值运算函数如下:

 MySQL之函数

 

 3.日期和时间函数

 有的时候们需要了解当前的时间,这时候我们就可以调用时间函数了。下面就是一些常用的时间函数:

 MySQL之函数

下面来说一下DATE_FORMAT(date,format):format可以有以下格式符:

Specifier    Description
%a             Abbreviated weekday name (Sun..Sat)
%b             Abbreviated month name (Jan..Dec)
%c             Month, numeric (0..12)
%D             Day of the month with English suffix (0th, 1st, 2nd, 3rd, …)
%d             Day of the month, numeric (00..31)
%e             Day of the month, numeric (0..31)
%f             Microseconds (000000..999999)
%H             Hour (00..23)
%h             Hour (01..12)
%I             Hour (01..12)
%i             Minutes, numeric (00..59)
%j             Day of year (001..366)
%k             Hour (0..23)
%l             Hour (1..12)
%M             Month name (January..December)
%m             Month, numeric (00..12)
%p             AM or PM
%r             Time, 12-hour (hh:mm:ss followed by AM or PM)
%S             Seconds (00..59)
%s             Seconds (00..59)
%T             Time, 24-hour (hh:mm:ss)
%U             Week (00..53), where Sunday is the first day of the week; WEEK() mode 0
%u             Week (00..53), where Monday is the first day of the week; WEEK() mode 1
%V             Week (01..53), where Sunday is the first day of the week; WEEK() mode 2; used with %X
%v             Week (01..53), where Monday is the first day of the week; WEEK() mode 3; used with %x
%W             Weekday name (Sunday..Saturday)
%w             Day of the week (0=Sunday..6=Saturday)
%X             Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%x             Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
%Y             Year, numeric, four digits
%y             Year, numeric (two digits)
%%             A literal % character
%x             x, for any “x” not listed above
格式符

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-05
  • 2021-12-30
  • 2021-05-30
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-04-12
  • 2021-10-28
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案