Chapter 5 Aggregate Functions and Built-in Functions

1. Aggregation function

1. Commonly used aggregation functions

insert image description here

2. Examples

(1)count

Count the number of students in the class.

insert image description here
Count the number of qq numbers collected by the class.
Before doing this requirement, we first create a table and insert certain data as shown in the figure below:
insert image description here
Now let's check how many qq numbers there are. As can be seen from the above figure, there are a total of 3 qq numbers.

insert image description here

(2)sum

The total score of the statistical mathematics achievement.
insert image description here

(3)avg

The average score of the total scores of the three subjects of Chinese, Mathematics and English in the statistical score sheet.
insert image description here

(4)max

Returns the highest score for the English score.
insert image description here

(5)min

Returns > 70+ minimum score in mathematics
insert image description here

2. Built-in functions

1. Date function

(1) Overview

insert image description here

(2) Example

Obtain year, month, day:
insert image description here
Obtain hour, minute, second:
insert image description here
Obtain timestamp:

insert image description here
Add a date to a date:
insert image description here
Subtract a date from a date:
insert image description here
Calculate the difference in days between two dates:
insert image description here

(3) Case

Let's first create a birthday table:
insert image description here

We then insert the data into the table with the current date as the birthday.
insert image description here

2. String functions

(1) Overview

insert image description here

(2) Example

Get the character set of the name of the exam_result table.
insert image description here
It is required to display the information in the exam_result table, and the display format is: "XXX has XXX points in Chinese, and XXX points in mathematics."
insert image description here
Replace all names with "Tang" in the exam_result table with "Beijing".
insert image description here

3. Mathematical functions

(1) Overview

insert image description here

(2) Example

Take the absolute value.
insert image description here
Rounded up.
insert image description here
Round down.
insert image description here
Keep 2 decimal places (decimals are rounded)

insert image description here

Generate random numbers
insert image description here

4. Other functions

1. user query current user

insert image description here

(2) md5(str) performs an md5 digest on a string str, and obtains a 32-bit string after the digest

insert image description here

(3) database() displays the database currently in use

insert image description here

(4) password() function, MySQL database uses this function to encrypt users

insert image description here

(5) ifnull(val1, val2) If val1 is null, return val2, otherwise return the value of val1

insert image description here

Guess you like

Origin blog.csdn.net/weixin_72060925/article/details/131902848