【四】【SQL】数学函数和其他函数

数学函数

|--------------------------------|----------------------|
| abs(number) | 绝对值函数 |
| bin(decimal_number) | 十进制转换二进制 |
| hex(decimal_number) | 转换十六进制 |
| conv(number,from_base,to_base) | 进制转换 |
| ceiling(number) | 向上取整 |
| floor(number) | 向下取整 |
| format(number,decimal_places) | 格式化,保留小数 |
| rand() | 返回随机浮点数,范围[0.0,1.0) |
| mod(number,denominator) | 取模,求余 |

Abs

复制代码
sql 复制代码
mysql> select abs(12);
+---------+
| abs(12) |
+---------+
|      12 |
+---------+
1 row in set (0.00 sec)

mysql> select abs(-12);
+----------+
| abs(-12) |
+----------+
|       12 |
+----------+
1 row in set (0.00 sec)

mysql> select abs(-12.3);
+------------+
| abs(-12.3) |
+------------+
|       12.3 |
+------------+
1 row in set (0.00 sec)

mysql> 

Bin

复制代码
sql 复制代码
mysql> select bin(10);
+---------+
| bin(10) |
+---------+
| 1010    |
+---------+
1 row in set (0.00 sec)

mysql> select bin(17);
+---------+
| bin(17) |
+---------+
| 10001   |
+---------+
1 row in set (0.00 sec)

mysql> select bin(20);
+---------+
| bin(20) |
+---------+
| 10100   |
+---------+
1 row in set (0.00 sec)

mysql> select bin(3.14);
+-----------+
| bin(3.14) |
+-----------+
| 11        |
+-----------+
1 row in set (0.00 sec)

mysql> select bin(7.14);
+-----------+
| bin(7.14) |
+-----------+
| 111       |
+-----------+
1 row in set (0.00 sec)

mysql> 

Hex

复制代码
sql 复制代码
mysql> select hex(15);
+---------+
| hex(15) |
+---------+
| F       |
+---------+
1 row in set (0.00 sec)

mysql> select hex(11);
+---------+
| hex(11) |
+---------+
| B       |
+---------+
1 row in set (0.00 sec)

mysql> select hex(16);
+---------+
| hex(16) |
+---------+
| 10      |
+---------+
1 row in set (0.00 sec)

mysql> select hex(255);
+----------+
| hex(255) |
+----------+
| FF       |
+----------+
1 row in set (0.00 sec)

mysql> 

Conv

复制代码
sql 复制代码
mysql> select conv(10,10,2);
+---------------+
| conv(10,10,2) |
+---------------+
| 1010          |
+---------------+
1 row in set (0.00 sec)

mysql> select conv(10,10,5);
+---------------+
| conv(10,10,5) |
+---------------+
| 20            |
+---------------+
1 row in set (0.00 sec)

mysql> select conv(10,10,4);
+---------------+
| conv(10,10,4) |
+---------------+
| 22            |
+---------------+
1 row in set (0.00 sec)

mysql> select conv(10,10,16);
+----------------+
| conv(10,10,16) |
+----------------+
| A              |
+----------------+
1 row in set (0.00 sec)

mysql> 

Format

复制代码
sql 复制代码
mysql> select format(3.1415926535,2);
+------------------------+
| format(3.1415926535,2) |
+------------------------+
| 3.14                   |
+------------------------+
1 row in set (0.00 sec)

mysql> select format(3.1415926535,3);
+------------------------+
| format(3.1415926535,3) |
+------------------------+
| 3.142                  |
+------------------------+
1 row in set (0.00 sec)

mysql> select format(3.1415926535,4);
+------------------------+
| format(3.1415926535,4) |
+------------------------+
| 3.1416                 |
+------------------------+
1 row in set (0.00 sec)

mysql> 

Mod

复制代码
sql 复制代码
mysql> select mod(10,3);
+-----------+
| mod(10,3) |
+-----------+
|         1 |
+-----------+
1 row in set (0.00 sec)

mysql> select mod(24,3);
+-----------+
| mod(24,3) |
+-----------+
|         0 |
+-----------+
1 row in set (0.00 sec)

mysql> select mod(24,4);
+-----------+
| mod(24,4) |
+-----------+
|         0 |
+-----------+
1 row in set (0.00 sec)

mysql> select mod(24,5);
+-----------+
| mod(24,5) |
+-----------+
|         4 |
+-----------+
1 row in set (0.00 sec)

mysql> 

Rand

复制代码
sql 复制代码
mysql> select rand();
+--------------------+
| rand()             |
+--------------------+
| 0.6462398373021184 |
+--------------------+
1 row in set (0.00 sec)

mysql> select rand();
+--------------------+
| rand()             |
+--------------------+
| 0.7120559184924289 |
+--------------------+
1 row in set (0.00 sec)

mysql> select rand();
+--------------------+
| rand()             |
+--------------------+
| 0.6215601112894342 |
+--------------------+
1 row in set (0.00 sec)

mysql> select rand();
+--------------------+
| rand()             |
+--------------------+
| 0.9716328317035295 |
+--------------------+
1 row in set (0.00 sec)

mysql> select rand()*100;
+-----------------+
| rand()*100      |
+-----------------+
| 99.348385538299 |
+-----------------+
1 row in set (0.00 sec)

mysql> select rand()*100;
+--------------------+
| rand()*100         |
+--------------------+
| 5.2520812538006165 |
+--------------------+
1 row in set (0.00 sec)

mysql> select rand()*100;
+--------------------+
| rand()*100         |
+--------------------+
| 28.215252727470595 |
+--------------------+
1 row in set (0.00 sec)

mysql> select format(rand()*100,1);
+----------------------+
| format(rand()*100,1) |
+----------------------+
| 25.3                 |
+----------------------+
1 row in set (0.00 sec)

mysql> select format(rand()*100,1);
+----------------------+
| format(rand()*100,1) |
+----------------------+
| 42.0                 |
+----------------------+
1 row in set (0.00 sec)

mysql> select format(rand()*100,0);
+----------------------+
| format(rand()*100,0) |
+----------------------+
| 34                   |
+----------------------+
1 row in set (0.00 sec)

mysql> select format(rand()*100,0);
+----------------------+
| format(rand()*100,0) |
+----------------------+
| 43                   |
+----------------------+
1 row in set (0.00 sec)

mysql> 
mysql> select format(rand()*1000,0);
+-----------------------+
| format(rand()*1000,0) |
+-----------------------+
| 145                   |
+-----------------------+
1 row in set (0.00 sec)

mysql> select format(rand()*1000,0);
+-----------------------+
| format(rand()*1000,0) |
+-----------------------+
| 431                   |
+-----------------------+
1 row in set (0.00 sec)

mysql> select format(rand()*1000,0);
+-----------------------+
| format(rand()*1000,0) |
+-----------------------+
| 720                   |
+-----------------------+
1 row in set (0.00 sec)

mysql> 

Ceiling

复制代码
sql 复制代码
mysql> select ceiling(3.1);
+--------------+
| ceiling(3.1) |
+--------------+
|            4 |
+--------------+
1 row in set (0.00 sec)

mysql> select ceiling(3.9);
+--------------+
| ceiling(3.9) |
+--------------+
|            4 |
+--------------+
1 row in set (0.00 sec)

mysql> select ceiling(3.009);
+----------------+
| ceiling(3.009) |
+----------------+
|              4 |
+----------------+
1 row in set (0.00 sec)

mysql> select ceiling(3.0);
+--------------+
| ceiling(3.0) |
+--------------+
|            3 |
+--------------+
1 row in set (0.00 sec)

mysql> select ceiling(3.99);
+---------------+
| ceiling(3.99) |
+---------------+
|             4 |
+---------------+
1 row in set (0.00 sec)

mysql> select ceiling(-3.99);
+----------------+
| ceiling(-3.99) |
+----------------+
|             -3 |
+----------------+
1 row in set (0.00 sec)

mysql> select ceiling(-3.1);
+---------------+
| ceiling(-3.1) |
+---------------+
|            -3 |
+---------------+
1 row in set (0.00 sec)

mysql> select ceiling(-3.1001);
+------------------+
| ceiling(-3.1001) |
+------------------+
|               -3 |
+------------------+
1 row in set (0.00 sec)

mysql> 

Floor

复制代码
sql 复制代码
ysql> select floor(4.5);
+------------+
| floor(4.5) |
+------------+
|          4 |
+------------+
1 row in set (0.00 sec)

mysql> select floor(4.1);
+------------+
| floor(4.1) |
+------------+
|          4 |
+------------+
1 row in set (0.00 sec)

mysql> select floor(4.001);
+--------------+
| floor(4.001) |
+--------------+
|            4 |
+--------------+
1 row in set (0.00 sec)

mysql> select floor(4.9);
+------------+
| floor(4.9) |
+------------+
|          4 |
+------------+
1 row in set (0.00 sec)

mysql> select floor(4.999);
+--------------+
| floor(4.999) |
+--------------+
|            4 |
+--------------+
1 row in set (0.00 sec)

mysql> select floor(-4.999);
+---------------+
| floor(-4.999) |
+---------------+
|            -5 |
+---------------+
1 row in set (0.00 sec)

mysql> select floor(-4.9);
+-------------+
| floor(-4.9) |
+-------------+
|          -5 |
+-------------+
1 row in set (0.00 sec)

mysql> select floor(-4.001);
+---------------+
| floor(-4.001) |
+---------------+
|            -5 |
+---------------+
1 row in set (0.00 sec)

mysql> select floor(-4.1);
+-------------+
| floor(-4.1) |
+-------------+
|          -5 |
+-------------+
1 row in set (0.00 sec)

mysql> 

其他函数

复制代码
sql 复制代码
mysql> select user();
+--------+
| user() |
+--------+
| root@  |
+--------+
1 row in set (0.00 sec)

mysql> select database();
+------------+
| database() |
+------------+
| scott      |
+------------+
1 row in set (0.00 sec)

mysql> create table user(
    -> id bigint primary key auto_increment,
    -> name varchar(20) not null,
    -> password char(32) not null
    -> );
Query OK, 0 rows affected (0.03 sec)

mysql> desc user;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | bigint(20)  | NO   | PRI | NULL    | auto_increment |
| name     | varchar(20) | NO   |     | NULL    |                |
| password | char(32)    | NO   |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> insert into user (name,password) values ('张三','abcd1234');
Query OK, 1 row affected (0.00 sec)

mysql> select *from user;
+----+--------+----------+
| id | name   | password |
+----+--------+----------+
|  1 | 张三   | abcd1234 |
+----+--------+----------+
1 row in set (0.00 sec)

mysql> insert into user(name,password) values ('李四',md5('hello'));
Query OK, 1 row affected (0.00 sec)

mysql> select *from user;
+----+--------+----------------------------------+
| id | name   | password                         |
+----+--------+----------------------------------+
|  1 | 张三   | abcd1234                         |
|  2 | 李四   | 5d41402abc4b2a76b9719d911017c592 |
+----+--------+----------------------------------+
2 rows in set (0.00 sec)

mysql> select md5('a');
+----------------------------------+
| md5('a')                         |
+----------------------------------+
| 0cc175b9c0f1b6a831c399e269772661 |
+----------------------------------+
1 row in set (0.00 sec)

mysql> select md5('123456');
+----------------------------------+
| md5('123456')                    |
+----------------------------------+
| e10adc3949ba59abbe56e057f20f883e |
+----------------------------------+
1 row in set (0.00 sec)

mysql> select name from user where name='李四' and password='hello';
Empty set (0.00 sec)

mysql> select name from user where name='张三' and password='abcd1234';
+--------+
| name   |
+--------+
| 张三   |
+--------+
1 row in set (0.00 sec)

mysql> select name from user where name='李四' and password=md5('hello');
+--------+
| name   |
+--------+
| 李四   |
+--------+
1 row in set (0.01 sec)

mysql> select password('1223');
+-------------------------------------------+
| password('1223')                          |
+-------------------------------------------+
| *0D7F7F31D85EF4BCE2C4BB738B9B3319F6D68B0F |
+-------------------------------------------+
1 row in set, 1 warning (0.02 sec)

mysql> select md5('hello');
+----------------------------------+
| md5('hello')                     |
+----------------------------------+
| 5d41402abc4b2a76b9719d911017c592 |
+----------------------------------+
1 row in set (0.00 sec)

mysql> select ifnull(null,10);
+-----------------+
| ifnull(null,10) |
+-----------------+
|              10 |
+-----------------+
1 row in set (0.00 sec)

mysql> select ifnull(null,10) result;
+--------+
| result |
+--------+
|     10 |
+--------+
1 row in set (0.00 sec)

mysql> select ifnull(20,10) result;
+--------+
| result |
+--------+
|     20 |
+--------+
1 row in set (0.00 sec)

mysql> select ifnull(20,null) result;
+--------+
| result |
+--------+
|     20 |
+--------+
1 row in set (0.00 sec)

mysql> 

结尾

最后,感谢您阅读我的文章,希望这些内容能够对您有所启发和帮助。如果您有任何问题或想要分享您的观点,请随时在评论区留言。

同时,不要忘记订阅我的博客以获取更多有趣的内容。在未来的文章中,我将继续探讨这个话题的不同方面,为您呈现更多深度和见解。

谢谢您的支持,期待与您在下一篇文章中再次相遇!

相关推荐
DONG9132 分钟前
《三驾马车:MySQL、MongoDB、Redis对比与融合实战》
数据库·redis·sql·mysql·mongodb·database
程序边界27 分钟前
从 Oracle 到 KingbaseES:企业信创改造的“抄作业”模板,直接套用!
数据库·oracle
funfan051737 分钟前
奇怪的“bug”--数据库的“隐式转换”行为
数据库·bug
Jasonakeke1 小时前
【重学MySQL】八十八、8.0版本核心新特性全解析
android·数据库·mysql
comeoffbest1 小时前
PostgreSQL 能存万物:从安装到高级功能实战
数据库·postgresql
时序数据说1 小时前
IoTDB如何解决海量数据存储难题?
大数据·数据库·物联网·时序数据库·iotdb
小楓12013 小时前
MySQL數據庫開發教學(二) 核心概念、重要指令
开发语言·数据库·mysql
花果山总钻风3 小时前
MySQL奔溃,InnoDB文件损坏修复记录
数据库·mysql·adb
TDengine (老段)4 小时前
TDengine IDMP 运维指南(管理策略)
大数据·数据库·物联网·ai·时序数据库·tdengine·涛思数据
Full Stack Developme4 小时前
PostgreSQL interval 转换为 int4 (整数)
数据库·postgresql