foreword
So far, we have almost learned the SQL statement. The last article talked about the basic operation of MySQL on data. If you don't know it yet, you can go and see it first.
Portal: [MySQL] Basic operations on data
In this article, let's summarize some of MySQL's built-in functions. Having these encapsulated functions will greatly improve our experience of using SQL statements. Let's start officially!
Numeric function
Digital functions, as the name suggests, are functions that operate on numbers and operate on numbers. There are mainly the following types:
Regarding FLOOR()
the AND CEIL()
function, it is actually rounded down and rounded up, as follows:
SELECT FLOOR(2.9);
SELECT CEIL(3.1);
Output:
PI()
This function does not require parameters, it directly obtains the pi for us to use, as follows:
SELECT PI();
Output: The parameters in the
trigonometric function are SIN(),COS(),TAN(),COT()
all angles in radians. When we generally find trigonometric functions, we use RADIANS()
functions to find them together, as follows:
SELECT SIN(RADIANS(30)),COS(RADIANS(30)),TAN(RADIANS(30));
output:
date function
Similarly, date functions operate on dates, and the standard format for a full date in MySQL is: yyyy-MM-dd hh:mmss
.
The most commonly used date functions are:
NOW()
Function: can get the system date and time, the return format isyyyy-MM-dd hh:mmss
CURDATE()
Function: can get the current system date, the return format isyyyy-MM-dd
CURTIME()
Function: can get the current system time, the return format ishh:mm:ss
In addition, we also use a DATE_FORMAT()
function to format the date and return the date format the user wants. The syntax is:
DELETE DATE_FORMAT(日期,表达式);
The expression here is a format expression that needs to be returned. Our commonly used expressions are as follows:
Example: today is the day of the year
SELECT DATE_FORMAT(NOW(),"%j");
Output:
Example: 2021-1-25 is the day of the week
SELECT DATE_FORMAT("2021-1-25","%w");
output:
character function
The character function is a function that operates on some characters, which are commonly used as follows: the
number of characters function LENGTH()
, Chinese and some are different, as follows:
SELECT LENGTH("CAsacCsdv");
Output:
This is the normal output, then look at Chinese:
SELECT LENGTH("白白,真帅");
Output:
Why is the result 13 instead of 5, what is going on here?
Because MySQL uses utf-8 encoding by default, and the length of a Chinese character in utf-8 encoding is three characters long, the final result is 13.
LPAD
There are RPAD
many such scenarios in our lives, such as:
Congratulations on winning the lottery for 12312345678 users. Generally speaking, we only display the tail number. We can LPAD
achieve this by using it as follows:
SELECT LPAD("5678",11,"*");
Output:
TRIM
The function removes spaces from the beginning and end of a string, as follows:
SELECT TRIM(" 白白 你真帅 ");
Output:
Only the head and tail are removed, and the spaces in the middle have no effect.
Condition function
Conditional judgments in programming languages can be implemented using conditional functions
IFNULL function
IFNULL(表达式, 值);
If the expression is not NULL
, IFNULL()
the return value is the expression itself; otherwise, it returns the value.
SELECT IF function
SELECT IF(expr1, expr2, expr3);
Returns if the expression expr1
holds expr2
, otherwise returns expr3
.
COALESCE function
SELECT COALESCE(表达式1,表达式2,....);
Returns the first non-empty expression in the argument (from left to right).
Conditional statements
Complex conditional judgments can be implemented with conditional statements, which are more powerful than IF
CADE
WHEN 表达式1 THEN 值1
WHEN 表达式2 THEN 值2
...
ELSE 值n
END;
CASE
Indicates the start of the END
function and the end of the function. Judging from the first one , if WHEN
it is satisfied , 表达式1
it will return 值1
, and if it does not match all expressions, it will return ELSE
the following value.
Epilogue
The basics of MySQL are basically finished here, and these contents can only be mastered after good practice.
Next notice: MySQL transaction mechanism
Continuing to update…