【四】【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> 

结尾

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

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

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

相关推荐
一 乐28 分钟前
考研论坛平台|考研论坛小程序系统|基于java和微信小程序的考研论坛平台小程序设计与实现(源码+数据库+文档)
java·数据库·学习·考研·微信·小程序·源码
一 乐29 分钟前
租拼车平台|小区租拼车管理|基于java的小区租拼车管理信息系统小程序设计与实现(源码+数据库+文档)
java·数据库·vue.js·微信·notepad++·拼车
BillDev40 分钟前
Oracle创建用户报错-ORA-65096: invalid common user or role name
数据库·oracle
szial1 小时前
深入理解SQL模糊查询中的特殊字符处理——以DB2为例
数据库·sql
jxxchallenger1 小时前
踩坑spring cloud gateway /actuator/gateway/refresh不生效
java·数据库·gateway
肥or胖1 小时前
【MySQL】索引
linux·数据库·mysql
Kiwi233331 小时前
产品经理-需求分析
数据库·产品经理·需求分析
青云交1 小时前
大数据新视界 --大数据大厂之数据质量评估指标与方法:提升数据可信度
大数据·sql·一致性·完整性·数据质量评估·数据可信度·准确性·时效性
读心悦2 小时前
修改 MySQL 数据库中的唯一键
数据库·mysql
qq_213157892 小时前
(c#)unity中sqlite多线程同时开启事务会导致非常慢
数据库·sqlite·c#