MySQL - Section 7 - MySQL Built-in Functions

Table of contents

1. Date function

1.1. Commonly used date functions

1.2. current_date function

1.3. current_time function

1.4. current_timestamp function

1.5. now function

1.6. date function

1.7. date_add function

1.8. date_sub function

1.9. datediff function

1.10. Comprehensive case

2. String functions

2.1. Commonly used string functions

2.2. charset function

2.3.concat function

2.4.instr function

2.5. ucase function

2.6. lcase function

2.7. left function

2.8. length function

2.9.replace function

2.10. strcmp function

2.11. substring function

2.12.ltrim, rtrim and trim functions

2.13. Comprehensive case

3. Mathematical functions

3.1. Commonly used mathematical functions

3.2.abs function

3.3.bin function

3.4.hex function

3.5.conv function

3.6.ceiling function

3.7. floor function

3.8. format function

3.9. rand function

3.10. mod function

4. Other functions

4.1. user function

4.2.md5 function

4.3. database function

4.4.password function

4.5. ifnull function


1. Date function

Aggregate functions can only appear in the select statement, and the date function here can appear in both the select statement and the where filter statement.

1.1. Commonly used date functions

Commonly used date functions are as follows:

1.2. current_date function

The current_date function is used to get the current date. as follows:

1.3. current_time function

The current_time function is used to get the current time. as follows:

1.4. current_timestamp function

The current_timestamp function is used to obtain the current timestamp and display it in date and time format. as follows:

1.5. now function

The now function is used to get the current date and time. as follows:

1.6. date function

The date function is used to get the current date and time. as follows:

1.7. date_add function

The date_add function is used to add a date or time based on a date. as follows:

If the date/time added in the date_add function is negative, it is equivalent to subtracting the date/time from the date. as follows:

1.8. date_sub function

The date_sub function is used to subtract a date or time from a date. as follows:

If the date/time subtracted in the date_sub function is negative, it is equivalent to adding the date/time to the base of the date. as follows:

1.9. datediff function

The datediff function is used to get the difference between two dates in days. as follows:

1.10. Comprehensive case

Create a comment table, which contains self-increasing primary key id, nickname, comment content and comment time. as follows:

Insert some data into the table, and specify the comment time directly through the now function when inserting. as follows:

When displaying comment information, if you only want to display the date of the comment but not the time of the comment, you can use the date function to intercept the date part of the sendtime when querying the sendtime field for display. as follows:

Insert some data into the table from time to time. as follows:

When displaying comment information, if you want to query the comment information published within 2 minutes, you actually need to filter out comments whose comment time plus 2 minutes is greater than the current time. At this time, you need to use the date_add and now functions at the same time. as follows:


2. String functions

Aggregate functions can only appear in the select statement, and the string functions here can appear in both the select statement and the where filter statement.

2.1. Commonly used string functions

Commonly used string functions are as follows:

2.2. charset function

The following employee table exists, and it is required to obtain the character set used by the ename column in the employee table. as follows:

When querying the information in the employee table, use the charset function to obtain the character set used by the ename column. as follows:

2.3.concat function

The following score sheet is currently available, and the information in the score sheet is required to be displayed in the format of "XXX's Chinese is XX points, mathematics is XX points, and English is XX points". as follows:

When querying the information in the grade table, use the concat function to perform string concatenation as required. as follows:

2.4.instr function

The instr function is used to get the position of the first occurrence of a string in another string, and returns 0 if it does not appear. as follows:

2.5. ucase function

The ucase function is used to obtain the string converted to uppercase. as follows:

2.6. lcase function

The lcase function is used to obtain the string converted to lowercase. as follows:

2.7. left function

The left function is used to intercept the specified number of characters backward from the left side of the string. as follows:

2.8. length function

The length function is used to get the number of bytes occupied by the string. as follows:

Note: For multi-byte characters, the number of bytes occupied by a character in different encodings is different. For example, a character in utf8 occupies 3 bytes, while a character in gbk occupies 2 bytes.

2.9.replace function

The replace function is used to replace a specified substring in a string with another string, for example, replace "S" in all names in the employee table with "Shanghai". as follows:

2.10. strcmp function

The strcmp function is used to compare the size of two strings character by character according to the ASCII code. The two strings are equal in size and return 0, the former returns 1, and the latter returns -1. as follows:

It should be noted that the strcmp function is not case-sensitive when comparing. as follows:

2.11. substring function

The substring function is used to intercept the specified number of characters backwards from the specified position of the string. as follows:

When using the substring function, if the number of characters to be intercepted is not specified, it will be intercepted from the specified position to the end by default. as follows:

For example, intercept the second to third characters of the ename field in the employee table. as follows:

2.12.ltrim, rtrim and trim functions

The trim function is used to remove leading and trailing spaces from a string. as follows:

The ltrim and rtrim functions are used to remove the leading and trailing spaces of a string, respectively. as follows:

2.13. Comprehensive case

It is required to display the names of all employees in the employee table in lowercase, the idea is as follows:

• Use the substring function to intercept the first character of the employee's name, and then use lcase to convert it to lowercase.
• Use the substring function to intercept the second and subsequent characters of the employee's name.
• Use the concat function to concatenate the first letter intercepted and converted to lowercase with the string intercepted the second time.
Finally, the string obtained after the concat function is concatenated is the employee name with the first letter lowercase. as follows:


3. Mathematical functions

Aggregate functions can only appear in the select statement, and the mathematical functions here can appear in both the select statement and the where filter statement.

3.1. Commonly used mathematical functions

Commonly used mathematical functions are as follows:

3.2.abs function

The abs function is used to obtain the absolute value of a number. as follows:

3.3.bin function

The bin function is used to convert a decimal number to binary. as follows:

3.4.hex function

The hex function is used to convert a decimal number to hexadecimal. as follows:

3.5.conv function

The conv function is used to convert a number from one base to another base. as follows:

3.6.ceiling function

The ceiling function is used to round up a number. as follows:

It should be noted that the essence of rounding up is to round towards positive infinity, so after negative numbers are rounded up, the first integer greater than or equal to this number is obtained. as follows:

3.7. floor function

The floor function is used to round down a number. as follows:

It should be noted that the essence of rounding down is to round towards negative infinity, so after negative numbers are rounded down, the first integer less than or equal to this number is obtained. as follows:

3.8. format function

The format function is used to format the value, rounding to retain the specified number of decimal places. as follows:

Note: The data after the execution of the format function will be output and displayed (it can be understood that the data after the execution of the format function becomes a string), and the numerical comparison of the data after the execution of the format function cannot be performed, as shown in the figure below.

3.9. rand function

The rand function is used to generate random floating point numbers from 0.0 to 1.0. as follows:

If you want to generate a random number from 0 to 100, you can multiply the generated random floating-point number by 100, and then round it in some way. as follows:

3.10. mod function

The mod function is used to perform remainder operations on numeric values. as follows:


4. Other functions

4.1. user function

The user function is used to get the current username and hostname of the MySQL connection. as follows:

4.2.md5 function

The md5 function is used to perform an md5 digest on a string, and obtain a 32-bit string after the digest. as follows:

expand:

• Generally, the company's internal database does not store the user's plaintext password, but forms a digest of the user password and stores the corresponding digest. When the user logs in to the account, the password entered by the user is digested and compared with the digest stored in the database. Compare, and allow login if the comparison is successful.
• There are two main advantages of doing this. The first advantage is that the company's internal database does not store the user's plaintext information. Even if the user information is leaked, it will not have much impact. Long, this is conducive to the design of the database table structure.

4.3. database function

The database function is used to display the database currently in use. as follows:

4.4.password function

The password function is used to encrypt user data. as follows:

Note: The encryption strength of the password function is higher than that of the md5 function, and mysql uses the password function internally.

4.5. ifnull function

The ifnull function accepts two parameters and returns the value of the first parameter if the first parameter is not null, otherwise returns the value of the second parameter. as follows:

Guess you like

Origin blog.csdn.net/qq_45113223/article/details/131351714