Mysql common functions and their usage

This article mainly explains the usage and meaning of mysql common functions, including string functions, numeric functions, date functions, and process functions.


1. String functions

① CONCAT(S1,S2,...,Sn): Concatenate S1 to Sn into a character string;
insert image description here

② LOWER(Str): Convert all strings Str to lowercase;
insert image description here

③ UPPER(Str): Convert the string Str to uppercase;
insert image description here

④ LPAD(Str,n,pad): Fill the left side of the string Str with the string pad to reach the length of n characters;
insert image description here

⑤ RPAD(Str,n,pad): Fill the right side of the string Str with the string pad to reach the length of n characters;
insert image description here

⑥ TRIM(Str): Remove the space at the head and tail of the character;
insert image description here

⑦ SUBSTRING(Str,Start,Len): Intercept the character string Str from Start to Len length.
insert image description here


2. Numeric functions

① CEIL(X): Round up;
insert image description here

② FLOOR(X): round down;
insert image description here

③ MOD(X,Y); returns the modulus of X/Y;
insert image description here
④ RAND(): returns a random number between 0 and 1;
insert image description here

⑤ ROUND(X,Y): Find the rounded value of X, and keep Y decimal places;
insert image description hereExample: Generate a 6-digit random verification code:
insert image description here


3. Date function

① CURDATE(): returns the current date;
insert image description here

② CURTIME(): returns the current time;
insert image description here

③ NOW(); returns the current date + time;
insert image description here

④ YEAR(date): Get the year of the specified date;
insert image description here

⑤ MONTH(date): Get the month of the specified date;
insert image description here

⑥ DAY(date): Get the number of days of the specified date;
insert image description here

⑦ DATE_add(date, INTERVEL expr type): returns the time value after a specified date/time date plus an expr value;
insert image description here

⑧ DATEDIFF(date1, date2): returns the number of days between two dates;
insert image description here


4. Process function

① IF(value, t, f): If the expression of value is true, return t, otherwise return;
insert image description here

② IFNULL(value1, value2): If value1 is not empty, return value1, otherwise return value2;
insert image description here

③ CASE WHEN [val1] THEN [res1]…ELSE [DEFAULT] END: If val1 is true, the return value is res1,… otherwise the return value is default; there are two tables, a student information table student
and a score table sc, the table is as follows , It is necessary to judge that the score greater than 80 is excellent, the score greater than 60 is pass, and the others are fail:
student table: insert image description here
score table:
insert image description here
write the sql statement as follows:

select s.*, (case when sc.score>80 then '优秀' when sc.score>60 then '及格' else '不及格' end )as 'level' from student as s
 left join sc on s.SId = sc.SId;

The execution result is:
insert image description here
④ CASE [expr] WHEN [val1] THEN [res1]...ELSE [defalut] END: If the value of expr is equal to val1, return res1, otherwise return the value defalut.

Guess you like

Origin blog.csdn.net/m0_37742400/article/details/131501029