MySQL basic knowledge learning - string function, numerical function, process function (2)

string functions

function effect
ASCII(char) Returns the ASCII code value of the character
BIT_LENGTH(str) Returns the bit length of the string
CONCAT(s1,s2…,sn) Concatenate s1,s2...,sn into a string
CONCAT_WS(sep, s1, s2..., sn) connects s1, s2..., sn into strings, and separates them with sep characters
INSERT(str,x,y,instr) Replace the substring of the string str starting from the xth position and y characters long with the string instr, and return the result
FIND_IN_SET(str,list) Analyze the comma-separated list list, if str is found, return the position of str in the list
LCASE(str)或LOWER(str) Returns the result of changing all characters in the string str to lowercase
LEFT(str,x) Returns the leftmost x characters in the string str
LENGTH(s) Returns the number of characters in the string str
LTRIM(str) Cut leading spaces from the string str
POSITION(substr,str) Returns the position of the first occurrence of the substring substr in the string str
QUOTE(str) escape single quotes in str with backslash
REPEAT(str,srchstr,rplcstr) Returns the result of repeating the string str x times
REVERSE(str) Returns the result of reversing the string str
RIGHT(str,x) Returns the rightmost x characters in the string str
RTRIM(str) Returns the space at the end of the string str
STRCMP(s1,s2) compares strings s1 and s2
TRIM(str) Remove all spaces from the beginning and end of a string
UCASE(str) or UPPER(str) Returns the result of converting all characters in the string str to uppercase
LPAD(str,n,pad) Left padding, fill the left side of str with the string pad to reach the length of n strings
RPAD(str,n,pad) Right padding, fill the right side of str with the string pad to reach the length of n strings
Substring(str,start,len) Returns a string of len lengths from the start position of the string str
select concat('hellow ', 'word');
select lower('Hellow');
select upper('hellow');
select LPAD('hellow', 10, '0');
select RPAD('hellow', 10, '0');
select trim('  hellow word  ');
select substring('hellow', 2, 3);

Numeric function

function effect
ABS(x) returns the absolute value of x
BIN(x) Returns the binary of x (OCT returns octal, HEX returns hexadecimal)
CEILING(x) Returns the smallest integer value greater than x
EXP(x) Returns the value e (the base of the natural logarithm) raised to the xth power
FLOOR(x) Returns the largest integer value less than x
GREATEST(x1,x2,…,xn) Returns the largest value in the collection
LEAST(x1,x2,…,xn) Returns the smallest value in the collection
LN(x) Returns the natural logarithm of x
LOG(x,y) Returns the base y logarithm of x
MOD(x,y) Returns the modulus (remainder) of x/y
PI() Returns the value of pi (pi)
RAND() Returns a random value between 0 and 1. You can make the RAND() random number generator generate a specified value by providing a parameter (seed).
ROUND(x,y) Returns the rounded value of x with y decimal places
SIGN(x) Returns the value representing the sign of the number x
SQRT(x) Returns the square root of a number
TRUNCATE(x,y) Returns the result of the number x truncated to y decimal places
select ceil(1.1);
select floor(1.9);
select mod(3,2);
select rand();
select round(2.335, 2);
// 取一个随机数6位验证码
select lpad(round(rand()*1000000, 0), 6, '0');

#date function

function effect
CURDATE()或CURRENT_DATE() returns the current date
CURTIME()或CURRENT_TIME() return the current time
DATE_ADD(date,INTERVAL int keyword) Return the result of date plus interval time int (int must be formatted according to keywords), such as: SELECTDATE_ADD(CURRENT_DATE, INTERVAL 6 MONTH);
DATE_FORMAT(date,fmt) Format the date value according to the specified fmt format
DATE_SUB(date,INTERVAL int keyword) Returns the result of subtracting the interval time int from the date date (the int must be formatted according to the keyword), such as: SELECTDATE_SUB(CURRENT_DATE, INTERVAL 6 MONTH);
DAYOFWEEK(date) Returns the day of the week represented by date (1~7)
DAYOFMONTH(date) The returned date is the day of the month (1~31)
DAYOFYEAR(date) The returned date is the day of the year (1~366)
DAYNAME(date) 返回date的星期名,如:SELECT DAYNAME(CURRENT_DATE);
FROM_UNIXTIME(ts,fmt) 根据指定的fmt格式,格式化UNIX时间戳ts
HOUR(time) 返回time的小时值(0~23)
MINUTE(time) 返回time的分钟值(0~59)
MONTH(date) 返回date的月份值(1~12)
MONTHNAME(date) 返回date的月份名,如:SELECT MONTHNAME(CURRENT_DATE);
NOW() 返回当前的日期和时间
QUARTER(date) 返回date在一年中的季度(1~4),如SELECT QUARTER(CURRENT_DATE);
WEEK(date) 返回日期date为一年中第几周(0~53)
YEAR(date) 返回日期date的年份(1000~9999)
Datediff(date1,date2) 返回日期date1与date2之间的天数
select curdate();
select curtime();
select now();
select year(now());
select month(now());
select day(now());
select date_add(now(), interval 30 day);
select datediff(now(), '2022-03-15') as '被封天数';

流程函数

函数 作用
CASE WHEN[test1] THEN [result1]…ELSE [default] END 如果test1是真,则返回result1,否则返回default
CASE [test] WHEN[val1] THEN [result]…ELSE [default]END 如果test和val1相等,则返回resultN,否则返回default
IF(test,t,f) 如果test是真,返回t;否则返回f
IFNULL(arg1,arg2) 如果arg1不是空,返回arg1,否则返回arg2
NULLIF(arg1,arg2) 如果arg1=arg2返回NULL;否则返回arg1
select if(1, 'true', 'errror');
select ifnull(null, 'default');
select ifnull(1, 'default');
select ifnull('', 'default');
select
    id,
    name,
    (case when age >= 16 then '成年' when age = 14 then '快成年' else '小屁孩' end) as '年龄',
    gender,
    nickname
    from tb_user;

Guess you like

Origin blog.csdn.net/weixin_45496521/article/details/128973006