MySQL数学函数简明总结_MySQL
1. ABS(x): 返回x的绝对值
代码如下:
mysql> select ABS(1), ABS(-1), ABS(0);
+--------+---------+--------+
| ABS(1) | ABS(-1) | ABS(0) |
+--------+---------+--------+
| 1 | 1 | 0 |
+--------+---------+--------+
2. PI(): 返回圆周率
代码如下:
mysql> select PI();
+----------+
| PI() |
+----------+
| 3.141593 |
+----------+
3. SQRT(x): 返回x的平方根,要求(x为非负数,返回NULL)
代码如下:
mysql> select SQRT(49), SQRT(0), SQRT(-49);
+----------+---------+-----------+
| SQRT(49) | SQRT(0) | SQRT(-49) |
+----------+---------+-----------+
| 7 | 0 | NULL |
+----------+---------+-----------+
4. MOD(x,y): 求余函数,返回x被y除后的余数;对于带有小数部分的数据值也起作用,它返回除法运算后的精确余数。
代码如下:
mysql> select MOD(31,8), MOD(21,-8), MOD(-7,2), MOD(-7,-2), MOD(45.5,6);
+-----------+------------+-----------+------------+-------------+
| MOD(31,8) | MOD(21,-8) | MOD(-7,2) | MOD(-7,-2) | MOD(45.5,6) |
+-----------+------------+-----------+------------+-------------+
| 7 | 5 | -1 | -1 | 3.5 |
+-----------+------------+-----------+------------+-------------+
5. CEIL(X): 返回不小X的最小整数值,返回值转为一个BIGINT.
代码如下:
mysql> select CEIL(-3.35), CEIL(3.35);
+-------------+------------+
| CEIL(-3.35) | CEIL(3.35) |
+-------------+------------+
| -3 | 4 |
+-------------+------------+
6. CEILING(X): 同CEIL(X)
代码如下:
mysql> select CEILING(-3.35), CEILING(3.35);
+----------------+---------------+
| CEILING(-3.35) | CEILING(3.35) |
+----------------+---------------+
| -3 | 4 |
+----------------+---------------+
7. FLOOR(X):返回不大于X的最大整数值,返回值转为一个BIGINT.
代码如下:
mysql> select FLOOR(-3.35), FLOOR(3.35);
+--------------+-------------+
| FLOOR(-3.35) | FLOOR(3.35) |
+--------------+-------------+
| -4 | 3 |
+--------------+-------------+
8. RAND()和RAND(X)
RAND(X) 返回一个随机浮点值,范围在0~1之间,X为整数,它被称作种子值,用来产生重复序列。即当X值相同时,产生的随机数也相同;
代码如下:
mysql> select RAND(10), RAND(10), RAND(2), RAND(-2);
+--------------------+--------------------+--------------------+--------------------+
| RAND(10) | RAND(10) | RAND(2) | RAND(-2) |
+--------------------+--------------------+--------------------+--------------------+
| 0.6570515219653505 | 0.6570515219653505 | 0.6555866465490187 | 0.6548542125661431 |
+--------------------+--------------------+--------------------+--------------------+
RAND(): 不带参数的RAND()每次产生不同0~1之间的随机数
代码如下:
mysql> SELECT RAND(), RAND(), RAND();
+--------------------+--------------------+---------------------+
| RAND() | RAND() | RAND() |
+--------------------+--------------------+---------------------+
| 0.6931893636409094 | 0.5147262984092592 | 0.49406343185721285 |
+--------------------+--------------------+---------------------+
9. ROUND(X)和ROUND(X,Y): 四舍五入函数,对X值按照Y进行四舍五入,Y可以省略,默认值为0;若Y不为0,则保留小数点后面指定Y位。
代码如下:
mysql> select ROUND(-1.14), ROUND(-1.9), ROUND(1.14), ROUND(1.9);
+--------------+-------------+-------------+------------+
| ROUND(-1.14) | ROUND(-1.9) | ROUND(1.14) | ROUND(1.9) |
+--------------+-------------+-------------+------------+
| -1 | -2 | 1 | 2 |
+--------------+-------------+-------------+------------+
mysql> select ROUND(1.38,1), ROUND(1.38,0), ROUND(232.38,-1), ROUND(232.38,-2);
+---------------+---------------+------------------+------------------+
| ROUND(1.38,1) | ROUND(1.38,0) | ROUND(232.38,-1) | ROUND(232.38,-2) |
+---------------+---------------+------------------+------------------+
| 1.4 | 1 | 230 | 200 |
+---------------+---------------+------------------+------------------+
10. TRUNCATE(X,Y): 与ROUND(X,Y)功能类似,但不进行四舍五入,只进行截取。
代码如下:
mysql> select TRUNCATE(1.33,1), TRUNCATE(1.99,1), TRUNCATE(1.99,0), TRUNCATE(19.99,-1);
+------------------+------------------+------------------+--------------------+
| TRUNCATE(1.33,1) | TRUNCATE(1.99,1) | TRUNCATE(1.99,0) | TRUNCATE(19.99,-1) |
+------------------+------------------+------------------+--------------------+
| 1.3 | 1.9 | 1 | 10 |
+------------------+------------------+------------------+--------------------+
11. SIGN(X): 返回参数X的符号,X的值为负、零或正数时返回结果依次为-1,0或1
代码如下:
mysql> select SIGN(-21), SIGN(-0),SIGN(0), SIGN(0.0), SIGN(21);
+-----------+----------+---------+-----------+----------+
| SIGN(-21) | SIGN(-0) | SIGN(0) | SIGN(0.0) | SIGN(21) |
+-----------+----------+---------+-----------+----------+
| -1 | 0 | 0 | 0 | 1 |
+-----------+----------+---------+-----------+----------+
12. POW(X,Y), POWER(X,Y)和EXP(X)
POW(X,Y)与POWER(X,Y)功能相同,用于返回X的Y次乘方的结果值
代码如下:
mysql> select pow(2,2), pow(2,-2), pow(-2,2), pow(-2,-2);
+----------+-----------+-----------+------------+
| pow(2,2) | pow(2,-2) | pow(-2,2) | pow(-2,-2) |
+----------+-----------+-----------+------------+
| 4 | 0.25 | 4 | 0.25 |
+----------+-----------+-----------+------------+
mysql> select power(2,2), power(2,-2), power(-2,2), power(-2,-2);
+------------+-------------+-------------+--------------+
| power(2,2) | power(2,-2) | power(-2,2) | power(-2,-2) |
+------------+-------------+-------------+--------------+
| 4 | 0.25 | 4 | 0.25 |
+------------+-------------+-------------+--------------+
EXP(X): 返回e的X乘方后的值:
代码如下:
mysql> select EXP(3), EXP(0), EXP(-3);
+-------------------+--------+---------------------+
| EXP(3) | EXP(0) | EXP(-3) |
+-------------------+--------+---------------------+
| 20.08553692318767 | 1 | 0.04978706836786393 |
+-------------------+--------+---------------------+
13. LOG(X)和LOG10(X): 对数运算函数(X必须为正数),LOG(X)-返回X的自然对数(X相对于基数e的对数) LOG10(X)-返回x的基数为10的对数:
代码如下:
mysql> select LOG(-3), LOG(0), LOG(3), LOG10(-100), LOG10(0), LOG10(100);
+---------+--------+--------------------+-------------+----------+------------+
| LOG(-3) | LOG(0) | LOG(3) | LOG10(-100) | LOG10(0) | LOG10(100) |
+---------+--------+--------------------+-------------+----------+------------+
| NULL | NULL | 1.0986122886681098 | NULL | NULL | 2 |
+---------+--------+--------------------+-------------+----------+------------+
14. RADIANS(X) 和 DEGREES(X): 角度与弧度转换函数
代码如下:
mysql> select RADIANS(90), RADIANS(180), DEGREES(PI()), DEGREES(PI()/2);
+--------------------+-------------------+---------------+-----------------+
| RADIANS(90) | RADIANS(180) | DEGREES(PI()) | DEGREES(PI()/2) |
+--------------------+-------------------+---------------+-----------------+
| 1.5707963267948966 | 3.141592653589793 | 180 | 90 |
+--------------------+-------------------+---------------+-----------------+
15. SIN(X), ASIN(X), COS(X), ACOS(X), TAN(X), ATAN(X), COT(X)
SIN(X): 正弦函数,其中X为弧度值
ASIN(X): 反正弦函数 其中X必须在-1到1之间
COS(X): 余弦函数,其中X为弧度值
ACOS(X): 反余弦函数 其中X必须在-1到1之间
TAN(X): 正切函数,其中X为弧度值
ATAN(X): 反正切函数,ATAN(X)与TAN(X)互为反函数
COT(X): 余切函数,函数COT和TAN互为倒函数
代码如下:
mysql> select SIGN(PI()/2),ASIN(1),COS(PI()), ACOS(-1), TAN(PI()/4), ATAN(1), COT(0.5);
+--------------+--------------------+-----------+-------------------+--------------------+--------------------+-------------------+
| SIGN(PI()/2) | ASIN(1) | COS(PI()) | ACOS(-1) | TAN(PI()/4) | ATAN(1) | COT(0.5) |
+--------------+--------------------+-----------+-------------------+--------------------+--------------------+-------------------+
| 1 | 1.5707963267948966 | -1 | 3.141592653589793 | 0.9999999999999999 | 0.7853981633974483 | 1.830487721712452 |
+--------------+--------------------+-----------+-------------------+--------------------+--------------------+-------------------+

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



There are many reasons why MySQL startup fails, and it can be diagnosed by checking the error log. Common causes include port conflicts (check port occupancy and modify configuration), permission issues (check service running user permissions), configuration file errors (check parameter settings), data directory corruption (restore data or rebuild table space), InnoDB table space issues (check ibdata1 files), plug-in loading failure (check error log). When solving problems, you should analyze them based on the error log, find the root cause of the problem, and develop the habit of backing up data regularly to prevent and solve problems.

MySQL uses shared locks and exclusive locks to manage concurrency, providing three lock types: table locks, row locks and page locks. Row locks can improve concurrency, and use the FOR UPDATE statement to add exclusive locks to rows. Pessimistic locks assume conflicts, and optimistic locks judge the data through the version number. Common lock table problems manifest as slow querying, use the SHOW PROCESSLIST command to view the queries held by the lock. Optimization measures include selecting appropriate indexes, reducing transaction scope, batch operations, and optimizing SQL statements.

In MySQL database operations, string processing is an inevitable link. The SUBSTRING_INDEX function is designed for this, which can efficiently extract substrings based on separators. SUBSTRING_INDEX function application example The following example shows the flexibility and practicality of the SUBSTRING_INDEX function: Extract specific parts from the URL For example, extract domain name: SELECTSUBSTRING_INDEX('www.mysql.com','.',2); Extract file extension to easily get file extension: SELECTSUBSTRING_INDEX('file.pdf','.',-1); Processing does not exist

MySQL can run without network connections for basic data storage and management. However, network connection is required for interaction with other systems, remote access, or using advanced features such as replication and clustering. Additionally, security measures (such as firewalls), performance optimization (choose the right network connection), and data backup are critical to connecting to the Internet.

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.

The MySQL primary key cannot be empty because the primary key is a key attribute that uniquely identifies each row in the database. If the primary key can be empty, the record cannot be uniquely identifies, which will lead to data confusion. When using self-incremental integer columns or UUIDs as primary keys, you should consider factors such as efficiency and space occupancy and choose an appropriate solution.

For production environments, a server is usually required to run MySQL, for reasons including performance, reliability, security, and scalability. Servers usually have more powerful hardware, redundant configurations and stricter security measures. For small, low-load applications, MySQL can be run on local machines, but resource consumption, security risks and maintenance costs need to be carefully considered. For greater reliability and security, MySQL should be deployed on cloud or other servers. Choosing the appropriate server configuration requires evaluation based on application load and data volume.

MySQL can return JSON data. The JSON_EXTRACT function extracts field values. For complex queries, you can consider using the WHERE clause to filter JSON data, but pay attention to its performance impact. MySQL's support for JSON is constantly increasing, and it is recommended to pay attention to the latest version and features.
