Summary of commonly used functions of MySQL query statements

Summary of commonly used functions of MySQL query statements

classification:

Single-line functions: such as concat, length, ifnull, etc.
Grouping functions: used for statistics, also known as statistical functions, aggregate functions, and group functions

One, one-line function

Character function

length: the number of bytes to obtain the parameter value
concat: concatenating the string
upper/lower: changing the string into uppercase/lowercase
substr/substring: intercepting the string
Note: the index starts from 1, intercepts all characters
instr after the specified index : Return the index of the first occurrence of the substring. If it cannot be found, return 0.
trim: Remove the spaces or substrings before and after the string.
lpad: Use the specified characters to fill the specified length.
rpad: Use the specified characters to fill the specified length.
replace: replace, replace all substrings

Mathematical function

round: round up,
ceil: round up, return >= the smallest integer of the parameter
floor: round down, return <= the largest integer of the parameter
truncate: truncated, truncated to a few digits after the decimal point
mod: take the remainder, the dividend is positive , Then it is positive; if the dividend is negative, then it is negative.
rand: Get a random number and return a decimal between 0-1

Date function

now: return the current system date + time
curdate: return the current system date, excluding the time
curtime: return the current time, excluding the date,
you can get the specified part, year, month, day, hour, minute, second
str_to_date: the date format Characters are converted into dates in the specified format

SELECT 
  STR_TO_DATE('1998-3-2', '%Y-%c-%d') AS output ;

date_format: Convert date to string

SELECT 
	DATE_FORMAT(NOW(), '%y年%m月%d日)') AS output ;

datediff: returns the number of days between two dates
monthname: returns the month in English

Flow control function

if function:

if(表达式1,表达式2,表达式3)
#如果表达式1成立,则if函数返回表达式2的值,否则返回表达式3的值


Case function: Case 1: Similar to the switch statement in java, generally used to achieve equivalence judgment.
Syntax:

when 常量1 then 要显示的值1或语句1;
when 常量2 then 要显示的值2或语句2;
...
else 要显示的值n或语句n;
end

Case 2: Similar to multiple if statements in java, generally used to realize interval judgment.
Syntax:

case
when 条件1 then 要显示的值1或语句1
when 条件2 then 要显示的值2或语句2
...
else 要显示的值n或语句n
end

Two, grouping function

Function: used for statistics, also known as aggregate function or statistical function or group function.
Classification: sum sum, avg average, max maximum, min minimum, count (not empty)
Features:
sum, avg are generally used Processing numerical data
max, min, count can handle any type of data The
above grouping functions ignore null values, and
can be combined with distinct to achieve deduplication operations

Guess you like

Origin blog.csdn.net/hyl1181/article/details/107726268