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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1252
29
C# Tutorial
1226
24
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.

Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

Can mysql and mariadb coexist Can mysql and mariadb coexist Apr 08, 2025 pm 02:27 PM

MySQL and MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.

RDS MySQL integration with Redshift zero ETL RDS MySQL integration with Redshift zero ETL Apr 08, 2025 pm 07:06 PM

Data Integration Simplification: AmazonRDSMySQL and Redshift's zero ETL integration Efficient data integration is at the heart of a data-driven organization. Traditional ETL (extract, convert, load) processes are complex and time-consuming, especially when integrating databases (such as AmazonRDSMySQL) with data warehouses (such as Redshift). However, AWS provides zero ETL integration solutions that have completely changed this situation, providing a simplified, near-real-time solution for data migration from RDSMySQL to Redshift. This article will dive into RDSMySQL zero ETL integration with Redshift, explaining how it works and the advantages it brings to data engineers and developers.

The relationship between mysql user and database The relationship between mysql user and database Apr 08, 2025 pm 07:15 PM

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

Laravel Eloquent ORM in Bangla partial model search) Laravel Eloquent ORM in Bangla partial model search) Apr 08, 2025 pm 02:06 PM

LaravelEloquent Model Retrieval: Easily obtaining database data EloquentORM provides a concise and easy-to-understand way to operate the database. This article will introduce various Eloquent model search techniques in detail to help you obtain data from the database efficiently. 1. Get all records. Use the all() method to get all records in the database table: useApp\Models\Post;$posts=Post::all(); This will return a collection. You can access data using foreach loop or other collection methods: foreach($postsas$post){echo$post->

MySQL: The Ease of Data Management for Beginners MySQL: The Ease of Data Management for Beginners Apr 09, 2025 am 12:07 AM

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

See all articles