MySQL function explanation (predicate, CASE)

Table of contents

MySQL common functions

String functions Functions for string manipulation

Arithmetic functions Functions that perform numerical calculations

Date functions Functions for date manipulation

Conversion functions Functions for converting data types and values

Process function for conditional deletion

Aggregation function A function for data aggregation

Predicates are generally used for conditional judgments


what is a function

A function is a program or code that can be called directly by another program

MySQL common functions

String functions Functions for string manipulation

MySQL's commonly used built-in string functions are as follows

CONCAT(S1,S2...Sn) String concatenation function, concatenate S1,S2...Sn into a string

LENGTH(str) string length, showing how many characters are contained in the string

LOWER(str) Convert the string str to lowercase

UPPER(str) Convert the string str to uppercase

REPLACE(str1,str2,str3) string replacement, replace the str2 part in str1 with str3

LPAD(str,n,pad) Left padding, fill the left side of str with the string pad, so that str reaches the length of n strings

RPAD(str,n,pad) Right padding, fill the right side of str with the string pad, so that str reaches the length of n strings

TRIM(str) removes leading and trailing spaces from the string

SUBSTRING(str,start,len) Returns a string of len lengths from the start position of the string str (the index of the string starts from 1, not 0)

update staff1 set number lpad ( number , 8 , '0' ) ;  #Modify the value of the number field, fill it with the string "0" to the left, so that the number reaches 8 string lengths (note: the data type of number is varchar)

Arithmetic functions Functions that perform numerical calculations

MySQL commonly used built-in arithmetic functions are as follows

+ (addition), - (subtraction), * (multiplication), / (division)

ABS(x) takes the absolute value of x

MOD(x,y) returns the modulus (remainder) of x/y

RAND() returns a random number within 0~1

ROUND(x,y) Find the rounded value of the parameter x, keep y decimal places

CEIL(x) round x up

FLOOR(x) round x down

select round ( rand ()* 100000 , 0 ) ;    #Randomly generate a 5-digit verification code If the generated decimal is 0.012345, the generated verification code is only 4 digits 1234, so you need to add 0
 select lpad ( round ( rand ()* 100000 , 0 ) , 5 , '0' ) #fill 0

Date functions Functions for date manipulation

MySQL's commonly used built-in date functions are as follows

CURDATE() returns the current date (year month day)

CURTIME() returns the current time

NOW() returns the current date and time

YEAR(date) Get the year of the specified date (year, month, day)

MONTH(date) Get the month of the specified date (year, month, day)

DAY(date) Get the number of days in the specified date (year, month, day)

DATE_ADD(date, INTERVAL expr type) Add expr on the specified date (year, month, day) (expr can be year/month/day, the specific type is determined by type)

DATEDIFF(date1, date2) returns the number of days between the start time date1 and the end time date2 (date1-date2)

select date_add ( now () , interval 20 day ) ;     #The date 20 days after the
 current date (month is month, year is year) select name , datediff ( curdate () , entrydate) as 'date' from staff1 order by date desc;     # Calculate the number of days of entry based on entry date and sort in reverse order

Conversion functions Functions for converting data types and values

CAST(data 1 AS data type 1) Convert data 1 to data type 1

COALESCE(data1, data2,...)

There are mainly two purposes: 1. Replace NULL with other values

2. Return the value of the first non-null expression in this data list

select cast ( '001' as signed integer ) ; #Convert the string "001" to a signed integer and display the result as 1
 select coalesce ( number , 0 ) from staff1 ; #Query    the number field of the staff1 table and replace the NULL value of this field with 0
 select coalesce ( number , age , 0 ) from staff1 ; #Query the number field of the staff1 table ; when numberis null , when age is not null , number returns the value of age ; when both number and age are null , returns 0 ; when number is non- null, returns the true value

Process function for conditional deletion

Process functions generally implement conditional filtering in SQL statements, thereby improving the efficiency of statements

MySQL commonly used built-in process functions are as follows

If value (conditional expression) is true, return t, otherwise return f

IF(vaule , t , f)                     

If value1 (can be a certain record) is not empty, return value1, responsible for returning value2

IFNULL(value1 , value2)      

If val1 is true, return res1; val2 is true, return res2; otherwise return default

CASE WHEN [ val1 ] THEN [res1] WHEN [val2] THEN [res2] …… ELSE [default] END

If the value of espr is equal to val1, return res1; the value of espr is equal to val2, return res2; otherwise return default

CASE [expr] WHEN [val1] THEN [res1]  WHEN [val2] THEN[res2] …… ELSE [default] END

select name , ( case origo when ' Chongqing ' then ' new first-tier city ' else ' non-first-tier city ' end ) from staff1 ; #Query the name and working city fields of the staff1 table , if the working city is Chongqing, it will be displayed as a new first-tier city, and the rest are non-first-tier cities

Aggregation function A function for data aggregation

The use of aggregate functions is introduced in the group query of DQL

Take a column of data as a whole for vertical calculation (all NULL values ​​do not participate in the calculation)

count statistics (how many values ​​a column has)

max maximum value (maximum value of a column)

min minimum value (minimum value of a column)

avg average (average value of a column)

sum sum (sum all values ​​in a column)

Aggregation functions use the format :

SELECT aggregate function (field list) AS [alias] FROM table name [WHERE condition list];

select COUNT ( * ) from emp ;    #Statistics of how many rows this table has (because Null does not participate in the statistics, the statistics of each column may be inconsistent, and the maximum value will be returned as the result)
 select max ( age ) from emp ;    #Statistics of the maximum value of
 age select avg ( age ) from emp ;    #Statistics of the average value of age

Predicates are generally used for conditional judgments

The use of predicates is introduced in the conditional query of DQL

Predicate, the full name is comparison predicate; it is a function that needs to satisfy the return value of True

BETWEEN … AND … within a certain range (left closed right closed)

IN(…) …is a plurality of information, in(…) means that as long as a certain information in… is satisfied, it is considered a match, and it is a true value

NOT IN(…) … is multiple information, as long as all the information of … is not satisfied, it will be considered as a match, and it is a true value

LIKE placeholder Fuzzy matching (_ means to match a single character, % means to match any character) (convenient usage of OR)

IS NULL means NULL value

IS NOT NULL indicates a non-NULL value

select * from staff1 where gender is null;      #query gender is null , and display the values ​​of all corresponding fields
 select * from staff1 where gender is not null; #query gender is non - null , and display the values ​​of all corresponding fields
 select * from staff1 where gender in ( ' male ' , ' female ' ) ;   And display the values ​​of all the fields corresponding to
 it select* from staff1 where name like '__'   ;       #query the information whose name is 2 characters, and display the values ​​of all the corresponding fields select
 * from staff1 where number between 20000100 and 20000102 #query the information corresponding to the number between 20000100 and 20000102 , and display the values ​​of all the corresponding fields

Precautions

For IN, if one of the two sides of this operator is NULL, the return result can only be NULL or 1; if no match is found, NULL is returned; if a match is found, 1 is returned;

For NOT IN, if one of the two sides of this operator is NULL, the return result can only be NULL or 0; if no match is found, NULL is returned; if a match is found, 0 is returned;

select null in ( 1 , 3 ) , null in ( 1 , 3 ,null ) ; #The result is null and null
 select 10 in ( 1 ,null ) , 10 in ( null, 10 ) ; #The result is null and 1
 select 10 in ( 1 , 20 ) , 10 in ( 1 , 10 );       #results are 0 and 1

select null not in ( 1 , 3 ) , null not in ( 1 , 3 ,null ) ; #The result is null and null
 select 10 not in ( 1 ,null ) , 10 not in ( null, 10 ) ; #The result is null and 0
 select 10 not in ( 1 , 20 ) , 10 not in ( 1 ,10 ) ;       #results are 1 and 0

MySQL Basic Grammar (DDL, DQL, DML, DCL )

Guess you like

Origin blog.csdn.net/m0_49864110/article/details/131927070