Mysql basic function 03-

function

String Functions

function usage
CONCAT(S1,S2,......,Sn) Connections S1, S2, ......, Sn as a string
CONCAT_WS(s, S1,S2,......,Sn) With CONCAT (s1, s2, ...) function, but with a s between each string
CHAR_LENGTH(s) Returns the number of characters in the string s
LENGTH(s) Returns the number of bytes in the string s, and related characters
INSERT(str, index , len, instr) Str string starting with the first position index, len characters long substring is replaced with a string instr
UPPER (s) or UCASE (s) The transfer of all the letters in the string s to uppercase
LOWER (s) or LCASE (s) All string of letters to lower case letters s,
LEFT(s,n) Returns the string s n leftmost characters
RIGHT(s,n) Returns the n rightmost character string s
LPAD (str, len, pad) Filling most of string str left pad, until the length of str len characters
RPAD (page only, pad) Filling of string str rightmost pad, until the length of str len characters
LTRIM(s) Strips spaces left string s
RTRIM(s) S strips spaces to the right of the string
TRIM(s) Strips spaces beginning and end of the string s
TRIM(【BOTH 】s1 FROM s) s1 remove beginning and end of the string s
TRIM(【LEADING】s1 FROM s) S1 removed at the beginning of the string s
TRIM(【TRAILING】s1 FROM s) S1 removed at the end of the string s
REPEAT(str, n) Str repeated n times return result
REPLACE(str, a, b) Replacing all occurrences of the string str string a string b
STRCMP(s1,s2) Compare strings s1, s2
SUBSTRING(s,index,len) Returns to its position index len characters string s
LOWER('SQL Course') Change the character to lowercase
UPPER('SQL Course')
Change to uppercase characters

Mathematical Functions

function usage
ABS(x) Returns the absolute value of x
CEIL(x) Returns the smallest integer greater than x
FLOOR(x) Returns the greatest integer less than x
V (x, y) Returns the remainder of x / y
RAND() Returns a random value between 0 and 1
ROUND(x,y) Returns the rounded decimal value parameter x bits are y
TRUNCATE(x,y) Returns the number x y bit result is truncated to decimal
SQRT(x) Returns the square root of x
POW (x, y) Y x to the power returns

Date Functions

function usage
CURDATE() 或 CURRENT_DATE() Returns the current date
CURTIME() 或 CURRENT_TIME() Returns the current time
NOW() / SYSDATE() / CURRENT_TIMESTAMP() / LOCALTIME() / LOCALTIMESTAMP() Returns the current system date and time
YEAR(date) / MONTH(date) / DAY(date) / HOUR(time) / MINUTE(time) / SECOND(time) Returns the value of the specific time
WEEK(date) / WEEKOFYEAR(date) The first few weeks of the year returns
DAYOFWEEK () Returns the day of the week, Note: Sunday is 1, Monday is 2 ,. . . Saturday is 7
WEEKDAY(date) Return of the week, it is noted Week 1 is 0, 1 Week 2 ,. . . Sunday is 6
DAYNAME(date) Day of the week: MONDAY, TUESDAY ..... SUNDAY
MONTHNAME(date) Returns the month: January ,. . . . .
DATEDIFF(date1,date2) / TIMEDIFF(time1, time2) Back date1 - date2 day interval / back time1 - time2 interval
DATE_ADD(datetime, INTERVAL expr type) Returns the difference between a given date and time date time period INTERVAL
DATE_FORMAT(datetime ,fmt) Fmt formatted as strings value date datetime
STR_TO_DATE(str, fmt) 按照字符串fmt对str进行解析,解析为一个日期

流程函数

函数 用法
IF(value,t ,f) 如果value是真,返回t,否则返回f
IFNULL(value1, value2) 如果value1不为空,返回value1,否则返回value2
CASE WHEN 条件1 THEN result1 WHEN 条件2 THEN result2 .... [ELSE resultn] END 相当于Java的if...else if...else...
CASE expr WHEN 常量值1 THEN 值1 WHEN 常量值1 THEN 值1 .... [ELSE 值n] END 相当于Java的switch...case...

其他函数

函数 用法
database() 返回当前数据库名
version() 返回当前数据库版本
user() 返回当前登录用户名
password(str) 返回字符串str的加密版本,41位长的字符串
md5(str) 返回字符串str的md5值,也是一种加密方式

单行函数

  • 只对一行进行变换,每行返回一个结果

  • 可以嵌套

  • 参数可以是一字段或一个表达式或一个值

分组函数

分组函数作用于一组数据,并对一组数据返回一个值。

可以对数值型数据使用AVG 和 SUM 函数。

可以对任意数据类型的数据使用 MIN 和 MAX 函数。

COUNT(*)返回表中记录总数,适用于任意数据类型

另:DATE_ADD(datetime,INTERVAL expr type)

表达式类型:

参数类型 参数类型
YEAR YEAR_MONTH
MONTH DAY_HOUR
DAY DAY_MINUTE
HOUR DAY_SECOND
MINUTE HOUR_MINUTE
SECOND HOUR_SECOND
  MINUTE_SECOND

DATE_FORMAT(datetime,fmt) 和 STR_TO_DATE(str, fmt)

表达式类型:

格式符 说明 格式符 说明
%Y 4位数字表示年份 %y 表示两位数字表示年份
%M 月名表示月份(January,....) %m 两位数字表示月份(01,02,03。。。)
%b 缩写的月名(Jan.,Feb.,....) %c 数字表示月份(1,2,3,...)
%D 英文后缀表示月中的天数(1st,2nd,3rd,...) %d 两位数字表示月中的天数(01,02...)
%e 数字形式表示月中的天数(1,2,3,4,5.....)    
%H 两位数字表示小数,24小时制(01,02..) %h和%I 两位数字表示小时,12小时制(01,02..)
%k 数字形式的小时,24小时制(1,2,3) %l 数字形式表示小时,12小时制(1,2,3,4....)
%i 两位数字表示分钟(00,01,02) %S和%s 两位数字表示秒(00,01,02...)
%W 一周中的星期名称(Sunday...) %a 一周中的星期缩写(Sun.,Mon.,Tues.,..)
%w 以数字表示周中的天数(0=Sunday,1=Monday....)    
%j 以3位数字表示年中的天数(001,002...) %U 以数字表示年中的第几周,(1,2,3。。)其中Sunday为周中第一天
%u 以数字表示年中的第几周,(1,2,3。。)其中Monday为周中第一天    
%T 24小时制 %r 12小时制
%p AM或PM %% 表示%

Guess you like

Origin www.cnblogs.com/Open-ing/p/12030474.html