[Learning MySQL from shallow to deep] MySQL common functions

foreword

Starting today, this series of content will take your friends to learn database technology. Database technology is an indispensable part of knowledge content in Java development. It is also a very important technology. This series of tutorials comprehensively explains the database system from the shallower to the deeper. It is very suitable for small partners with zero foundation to learn.


The full text is about [1268] words, no nonsense, just pure dry stuff that allows you to learn techniques and understand principles! This article has a wealth of cases and pictures, so that you can better understand and use the technical concepts in the article, and can bring you enough enlightening thinking...

1. Time function

Dear friends, the following summarizes the functions of operating time in MySQL, that is, the method of operating time. The syntax is as follows:

语法:SELECT 时间函数([参数列表])

Experience: Executing the time function query will automatically generate a virtual table (one row and one column).
Please add a picture description

Chestnuts below: Get the current system time.

#查询当前时间
SELECT SYSDATE();

image.png

#查询当前时间
SELECT NOW();

image.png

#获取当前日期
SELECT CURDATE();

image.png

#获取当前时间
SELECT CURTIME();

image.png

2. String functions

Next, I will introduce functions for manipulating strings . In SQL statements, you can concatenate strings, calculate lengths, convert uppercase to lowercase, and convert lowercase to uppercase.

语法: SELECT 字符串函数 ([参数列表])

String functions illustrate
CONCAT(str1,str2,str…) concatenate multiple strings
INSERT(str,pos,len,newStr) Replace the content of len length starting from the specified pos position in str with newStr
LOWER(str) Convert the specified string to lowercase
UPPER(str) Convert the specified string to uppercase
SUBSTRING(str,num,len) Specify the num position of the str string to start intercepting len content

Give chestnuts: string applications.

#拼接内容
SELECT CONCAT('My','S','QL');

image.png

#字符串替换
SELECT INSERT('这是一个数据库',3,2,'MySql'); #结果为这是 MySql 数据库

image.png

#指定内容转换为小写
SELECT LOWER('MYSQL');#mysql

image.png

#指定内容转换为大写
SELECT UPPER('mysql');#MYSQL

image.png

#指定内容截取
SELECT SUBSTRING('JavaMySQLOracle',5,5);#MySQL

image.png

3. Aggregate functions

Aggregation functions are the most frequently used functions that we can use every day, that is, functions such as summation, calculation of the total number of records, average value, maximum value, and minimum value.

语法:SELECT 聚合函数(列名) FROM 表名;

Experience: Perform statistics on a single column of multiple pieces of data, and return a row of results after statistics.

aggregate function illustrate
SUM() Sum the results of a single column across all rows
AVG() average value
MAX() maximum value
MIN() minimum value
COUNT() Find the total number of rows

Give chestnuts: Find the sum.

#统计所有学生年龄总和
SELECT SUM(Sage) FROM student;

image.png

Citing chestnuts: find the average.

#统计学生中年龄最大的学员
SELECT MAX(Sage) FROM student;

image.png

#统计学生中年龄最小的学员
SELECT MIN(Sage) FROM student;

image.png

#统计学生总数
SELECT COUNT(*) FROM student;

image.png

Note: Aggregation functions automatically ignore null values ​​and do not perform statistics.


4. Conclusion

Finally, here is a summary of the core points of this article:

  1. The functions commonly used in MySQL are divided into three categories: date manipulation functions, string manipulation functions, and aggregation functions.

  2. Aggregation functions are the most important and are used most of the time. Friends, you should practice the use of aggregation functions more.

Guess you like

Origin blog.csdn.net/GUDUzhongliang/article/details/130600550