Home Database Mysql Tutorial Summary of MySQL commonly used functions

Summary of MySQL commonly used functions

Feb 17, 2021 am 10:04 AM

Summary of MySQL commonly used functions

Free learning recommendation: mysql video tutorial

##this This article explains the function methods of MySQL, covering all common MySQL methods. The following is the directory structure of this article. You can click on the directory on the right to search according to your needs:

    1. Numeric functions
  • 2. String functions
  • 3 , Date function
  • 4. MySQL advanced functions

(1) Numeric function

1. ABS(x) Return The absolute value of x

SELECT ABS(-1);
---- 返回1
Copy after login

2. AVG(expression) returns the average of an expression, expression is a field
Summary of MySQL commonly used functions

SELECT AVG(age) FROM student;
Copy after login

Summary of MySQL commonly used functions

3. CEIL(x)/CEILING(x) returns the smallest integer greater than or equal to x

SELECT CEIL(1.5);
SELECT CEILING(1.5);   
---- 返回2
Copy after login

4.FLOOR(x ) Returns the largest integer less than or equal to x

SELECT FLOOR(1.5); 
---- 返回1
Copy after login

5, EXP(x) Returns e raised to the power of x

SELECT EXP(3);
---- 计算 e 的三次方,返回20.085536923188
Copy after login

6, GREATEST (expr1, expr2, expr3, …) Returns the maximum value in the list

SELECT GREATEST(3, 12, 34, 8, 25);
---- 返回以下数字列表中的最大值34
Copy after login

7, LEAST(expr1, expr2, expr3, …) Returns the minimum value in the list

SELECT LEAST(3, 12, 34, 8, 25);
---- 返回以下数字列表中的最小值3
Copy after login

8. LN returns the natural logarithm of the number

SELECT LN(2);
---- 返回 2 的自然对数:0.6931471805599453
Copy after login

9. LOG(x) returns the natural logarithm (logarithm with base e)

SELECT LOG(20.085536923188);
---- 返回 3
Copy after login

10. MAX(expression) returns the maximum value in field expression

SELECT MAX(age) AS maxAge FROM Student;
---- age最大值
Copy after login

11.MIN(expression) returns the maximum value in field expression Maximum value

SELECT MIN(age) AS minAge FROM Student;
---- age最小值
Copy after login

12. POW(x,y)/POWER(x,y) returns x raised to the power of y

SELECT POW(2,3);
SELECT POWER(2,3);
---- 返回2 的 3 次方:8
Copy after login

13 , RAND() returns a random number from 0 to 1

SELECT RAND();
---- 返回 0 到 1 的随机数,若()里面有数字,RAND(x),x相同时,返回值相同
Copy after login

14, ROUND(x) returns the nearest integer to x

SELECT ROUND(1.23456);
---- 返回 1
Copy after login

15 , SIGN(x) returns the sign of x, x is a negative number, 0, and positive number returns -1, 0 and 1 respectively

SELECT SIGN(-10);
---- 返回 -1
Copy after login

16. SQRT(x) returns the square root of x

SELECT SQRT(25);
---- 返回5
Copy after login

17. SUM(expression) returns the sum of the specified field

SELECT SUM(age) AS totalAage FROM Student;
---- 返回age的总和
Copy after login

18. TRUNCATE(x,y) returns the value x to the decimal point The value of the last y digit (the biggest difference from ROUND is that it will not be rounded)

SELECT TRUNCATE(1.23456,3);
---- 返回1.234
Copy after login

(2) String function

1, Returns the ASCII code of the first character of string s

SELECT ASCII('AB');
---- 返回A的ASCII码值:65
Copy after login

2. LENGTH/CHAR_LENGTH(s)/CHARACTER_LENGTH(s) returns the number of characters of string s

SELECT LENGTH('1234');
---- 返回4
Copy after login

3. CONCAT(s1,s2…sn) strings s1, s2 and other strings are combined into one string

SELECT CONCAT('hel','llo');
---- 返回hello
Copy after login

4.FIND_IN_SET( s1, s2) returns the position of the string matching s1 in string s2

SELECT FIND_IN_SET("c", "a,b,c,d,e");
---- 返回3
Copy after login

5. The FORMAT(x,n) function can format the number x "#, .##", keep x to n digits after the decimal point, and round the last digit

SELECT FORMAT(250500.5634, 2); 
---- 返回250,500.56
Copy after login

6, INSERT(s1,x,len,s2) string s2 replacement The x position of s1 starts a string of length len

SELECT INSERT("google.com", 1, 6, "runnob");
---- 返回runoob.com
Copy after login

7. LOCATE(s1,s) gets the starting position of s1 from the string s

SELECT LOCATE('st','myteststring');
---- 返回5
Copy after login

8, LCASE(s)/LOWER(s) turns all letters of string s into lowercase letters

SELECT LOWER('RUNOOB');
---- 返回runoob
Copy after login

9, UCASE(s)/UPPER(s )Convert all letters of string s into uppercase letters

SELECT UCASE('runoob');
---- 返回RUNOOB
Copy after login

10. TRIM(s) remove the spaces at the beginning and end of string s

SELECT TRIM('    RUNOOB    ');
---- 返回RUNOOB
Copy after login

11. LTRIM(s) removes the spaces at the beginning of the string s

SELECT LTRIM('    RUNOOB    ');
---- 返回 ’RUNOOB   ‘
Copy after login

12. RTRIM(s) removes the spaces at the end of the string s

SELECT RTRIM('    RUNOOB    ');
---- 返回 ’    RUNOOB‘
Copy after login

13. SUBSTR(s, start, length) intercepts a substring of length length from the start position of string s

SELECT SUBSTR("RUNOOB", 2, 3) AS ExtractString;
---- 从字符串 RUNOOB 中的第 2 个位置截取 3个 字符,返回UNO
Copy after login

14.SUBSTR/ SUBSTRING(s, start, length) intercepts a substring of length from the start position of string s

SELECT SUBSTR/SUBSTRING("RUNOOB", 2, 3);
---- 从字符串 RUNOOB 中的第 2 个位置截取 3个 字符,返回UNO
Copy after login

15. POSITION(s1 IN s) obtains from string s The starting position of s1

SELECT POSITION('b' in 'abc');
---- 返回2
Copy after login

16. REPEAT(s,n) repeats the string s n times

SELECT REPEAT('runoob',3);
---- 返回runoobrunoobrunoob
Copy after login

17.REVERSE(s )Reverse the order of string s

SELECT REVERSE('abc');
---- 返回cba
Copy after login

18. STRCMP(s1,s2) compares strings s1 and s2. If s1 and s2 are equal, return 0. If s1>s2, return 1. If s1

SELECT STRCMP("runoob", "runoob");
---- 返回0
Copy after login

(3) Date function

1, CURDATE()/CURRENT_DATE() returns the current Date

SELECT CURDATE();
SELECT CURRENT_DATE();
---- 返回2019-02-19
Copy after login

2. CURRENT_TIME()/CURTIME() returns the current time

SELECT CURRENT_TIME();
---- 返回11:40:45
Copy after login

3. CURRENT_TIMESTAMP() returns the current date and time

SELECT CURRENT_TIMESTAMP();
---- 返回2019-02-19 11:41:32
Copy after login

4. ADDDATE(d,n) calculates the start date d plus n days’ date

SELECT ADDDATE("2017-06-15", INTERVAL 10 DAY);
---- 返回2017-06-25
Copy after login

5. ADDTIME(t, n) Time t plus n seconds of time

SELECT ADDTIME('2011-11-11 11:11:11', 5);
---- 返回2011-11-11 11:11:16
Copy after login

6. DATE() extracts the date value from the date or datetime expression

SELECT DATE("2017-06-15 11:11:16");
---- 返回2017-06-15
Copy after login

7. DAY(d) returns the date part of date value d

SELECT DAY("2017-06-15"); 
---- 返回15
Copy after login

8、DATEDIFF(d1,d2)计算日期 d1->d2 之间相隔的天数

SELECT DATEDIFF('2001-01-01','2001-02-02'); 
---- 返回-32
Copy after login

9、DATE_FORMAT按表达式 f的要求显示日期 d

SELECT DATE_FORMAT('2011.11.11 11:11:11','%Y-%m-%d %r');
---- 返回2011-11-11 11:11:11 AM
Copy after login

10、DAYNAME(d)返回日期 d 是星期几,如 Monday,Tuesday

SELECT DAYNAME('2011-11-11 11:11:11');
---- 返回Friday
Copy after login

11、DAYOFMONTH(d)计算日期 d 是本月的第几天

SELECT DAYOFMONTH('2011-11-11 11:11:11');
---- 返回11
Copy after login

12、DAYOFWEEK(d)日期 d 今天是星期几,1 星期日,2 星期一,以此类推

SELECT DAYOFWEEK('2011-11-11 11:11:11');
---- 返回6
Copy after login
Copy after login

13、DAYOFYEAR(d)计算日期 d 是本年的第几天

SELECT DAYOFYEAR('2011-11-11 11:11:11');
---- 返回315
Copy after login

14、EXTRACT(type FROM d)从日期 d 中获取指定的值,type 指定返回的值
type可取值为:
MICROSECOND
SECOND
MINUTE
HOUR
DAY
WEEK
MONTH
QUARTER
YEAR
SECOND_MICROSECOND
MINUTE_MICROSECOND
MINUTE_SECOND
HOUR_MICROSECOND
HOUR_SECOND
HOUR_MINUTE
DAY_MICROSECOND
DAY_SECOND
DAY_MINUTE
DAY_HOUR
YEAR_MONTH

SELECT EXTRACT(MINUTE FROM '2011-12-13 14:15:16');
---- 返回15
Copy after login

15、DAYOFWEEK(d)日期 d 今天是星期几,1 星期日,2 星期一,以此类推

SELECT DAYOFWEEK('2011-11-11 11:11:11');
---- 返回6
Copy after login
Copy after login

16、UNIX_TIMESTAMP()得到时间戳

SELECT UNIX_TIMESTAMP('2019-2-19');
SELECT UNIX_TIMESTAMP(expression);
---- 返回1550505600
Copy after login

17、FROM_UNIXTIME()时间戳转日期

SELECT FROM_UNIXTIME(1550505600);
---- 返回2019-02-19 00:00:00
SELECT FROM_UNIXTIME(1550505600, '%Y-%m-%d');
---- 返回2019-02-19
Copy after login

(四)MySQL高级函数

1、IF(expr,v1,v2)如果表达式 expr 成立,返回结果 v1;否则,返回结果 v2

SELECT IF(1>0,'yes','no');
---- 返回yes
Copy after login

2、CONV(x,f1,f2)返回 f1 进制数变成 f2 进制数

SELECT CONV(13,10,2);
---- 返回1101
Copy after login

3、CURRENT_USER()/SESSION_USER()/SYSTEM_USER()/USER()返回当前用户
4、DATABASE()返回当前数据库名
5、VERSION()返回数据库的版本号

有不正确的地方,欢迎前来指正!

相关免费学习推荐:mysql数据库(视频)

The above is the detailed content of Summary of MySQL commonly used functions. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

How do you handle large datasets in MySQL? How do you handle large datasets in MySQL? Mar 21, 2025 pm 12:15 PM

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

Difference between clustered index and non-clustered index (secondary index) in InnoDB. Difference between clustered index and non-clustered index (secondary index) in InnoDB. Apr 02, 2025 pm 06:25 PM

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values ​​and pointers to data rows, and is suitable for non-primary key column queries.

When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

How do you drop a table in MySQL using the DROP TABLE statement? How do you drop a table in MySQL using the DROP TABLE statement? Mar 19, 2025 pm 03:52 PM

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

See all articles