[MySQL] The use of MySQL basic functions

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:
insert image description here
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:
insert image description here
insert image description here
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
insert image description here
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:
insert image description here

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 is yyyy-MM-dd hh:mmss
CURDATE()Function: can get the current system date, the return format is yyyy-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:
insert image description here
Example: today is the day of the year

SELECT DATE_FORMAT(NOW(),"%j");

Output:
insert image description here
Example: 2021-1-25 is the day of the week

SELECT DATE_FORMAT("2021-1-25","%w");

output:
insert image description here

character function

The character function is a function that operates on some characters, which are commonly used as follows: the
insert image description here
number of characters function LENGTH(), Chinese and some are different, as follows:

SELECT LENGTH("CAsacCsdv");

Output:
insert image description here
This is the normal output, then look at Chinese:

SELECT LENGTH("白白,真帅");

Output:
insert image description here
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.

LPADThere are RPADmany 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 LPADachieve this by using it as follows:

SELECT LPAD("5678",11,"*");

Output:
insert image description here
TRIMThe function removes spaces from the beginning and end of a string, as follows:

SELECT TRIM("        白白  你真帅   ");

Output:
insert image description here
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 expr1holds 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 THEN1
	WHEN 表达式2 THEN2
	...
	ELSE 值n 
END;

CASEIndicates the start of the ENDfunction and the end of the function. Judging from the first one , if WHENit is satisfied , 表达式1it will return 值1, and if it does not match all expressions, it will return ELSEthe 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…

Guess you like

Origin blog.csdn.net/apple_51673523/article/details/122528591