美文网首页
MySQL 常用函数

MySQL 常用函数

作者: Vincent_Jiang | 来源:发表于2017-06-23 14:11 被阅读57次

RAND() 函数

MySQL RAND() 函数可以被调用,产生一个在 0 和 1 之间的浮点数

mysql> SELECT RAND(), RAND(), RAND();
+--------------------+--------------------+--------------------+
| RAND()             | RAND()             | RAND()             |
+--------------------+--------------------+--------------------+
| 0.5983982343211753 | 0.8651665978740589 | 0.5306383171404138 |
+--------------------+--------------------+--------------------+
1 row in set (0.00 sec)

当使用整数作为参数调用时,RAND() 使用该值作为随机数的种子发生器。每次种子使用给定值生成,RAND() 将产生一个可重复的系列数字

mysql> SELECT RAND(1), RAND(1), RAND();
+------------------+------------------+------------------+
| RAND(1)          | RAND()           | RAND()           |
+------------------+------------------+------------------+
| 0.18109050223705 | 0.75023211143001 | 0.20788908117254 |
+------------------+------------------+------------------+
1 row in set (0.00 sec)

使用 ORDER BY RAND() 语句还可以对表中的数据进行随机读取

mysql> SELECT * FROM employee_tbl ORDER BY RAND();
+------+------+------------+--------------------+
| id   | name | work_date  | daily_typing_pages |
+------+------+------------+--------------------+
|    5 | Zara | 2007-06-06 |                300 |
|    3 | Jack | 2007-04-06 |                100 |
|    3 | Jack | 2007-05-06 |                170 |
|    2 | Ram  | 2007-05-27 |                220 |
|    4 | Jill | 2007-04-06 |                220 |
|    5 | Zara | 2007-02-06 |                350 |
|    1 | John | 2007-01-24 |                250 |
+------+------+------------+--------------------+
7 rows in set (0.01 sec)

mysql> SELECT * FROM employee_tbl ORDER BY RAND();
+------+------+------------+--------------------+
| id   | name | work_date  | daily_typing_pages |
+------+------+------------+--------------------+
|    5 | Zara | 2007-02-06 |                350 |
|    2 | Ram  | 2007-05-27 |                220 |
|    3 | Jack | 2007-04-06 |                100 |
|    1 | John | 2007-01-24 |                250 |
|    4 | Jill | 2007-04-06 |                220 |
|    3 | Jack | 2007-05-06 |                170 |
|    5 | Zara | 2007-06-06 |                300 |
+------+------+------------+--------------------+
7 rows in set (0.00 sec)

FLOOR()、ROUND() 函数

mysql> SELECT FLOOR(1.58), FLOOR(-1.58), FLOOR(0);
+-------------+--------------+----------+
| FLOOR(1.58) | FLOOR(-1.58) | FLOOR(0) |
+-------------+--------------+----------+
|           1 |           -2 |        0 |
+-------------+--------------+----------+
1 row in set (0.00 sec)
mysql> SELECT ROUND(-1.23), ROUND(-1.58), ROUND(1.58), ROUND(1.298, 1);
+--------------+--------------+-------------+-----------------+
| ROUND(-1.23) | ROUND(-1.58) | ROUND(1.58) | ROUND(1.298, 1) |
+--------------+--------------+-------------+-----------------+
| -1           | -2           | 2           | 1.3             |
+--------------+--------------+-------------+-----------------+
1 row in set (0.00 sec)
mysql> SELECT ROUND(1.298, 0), ROUND(23.298, -1), ROUND(150.000,2), ROUND(150,2);
+-----------------+-------------------+------------------+--------------+
| ROUND(1.298, 0) | ROUND(23.298, -1) | ROUND(150.000,2) | ROUND(150,2) |
+-----------------+-------------------+------------------+--------------+
| 1               | 20                | 150              |          150 |
+-----------------+-------------------+------------------+--------------+
1 row in set (0.00 sec)

MD5() 函数

mysql> SELECT MD5(NULL), MD5('123456');
+-----------+----------------------------------+
| MD5(NULL) | MD5('123456')                    |
+-----------+----------------------------------+
| NULL      | e10adc3949ba59abbe56e057f20f883e |
+-----------+----------------------------------+
1 row in set (0.00 sec)

生成一到一百的随机数字

mysql> SELECT FLOOR(RAND() * 100 + 0), FLOOR(RAND() * 100 + 0);
+-------------------------+-------------------------+
| FLOOR(RAND() * 100 + 0) | FLOOR(RAND() * 100 + 0) |
+-------------------------+-------------------------+
|                      48 |                      81 |
+-------------------------+-------------------------+
1 row in set (0.00 sec)

MD5 生成随机字符串

mysql> SELECT SUBSTRING(MD5(RAND()), 1, 24);
+-------------------------------+
| SUBSTRING(MD5(RAND()), 1, 24) |
+-------------------------------+
| 46939819a45d1166e4e8f184      |
+-------------------------------+
1 row in set (0.00 sec)

FROM_UNIXTIME() 函数

mysql> SELECT 
    -> FROM_UNIXTIME(-1) AS 'A',
    -> FROM_UNIXTIME(0) AS 'B',
    -> FROM_UNIXTIME(1483200000) AS 'C',
    -> FROM_UNIXTIME(1483200000, '%Y-%m-%d') AS 'D';
+------+---------------------+---------------------+------------+
| A    | B                   | C                   | D          |
+------+---------------------+---------------------+------------+
| NULL | 1970-01-01 08:00:00 | 2017-01-01 00:00:00 | 2017-01-01 |
+------+---------------------+---------------------+------------+

生成 2017-01-01 ~ 2018-01-01 范围内的随机日期

mysql> SELECT FROM_UNIXTIME(1483200000 + ROUND(RAND() * 60 * 60 * 24 * 365));
+----------------------------------------------------------------+
| FROM_UNIXTIME(1483200000 + ROUND(RAND() * 60 * 60 * 24 * 365)) |
+----------------------------------------------------------------+
| 2017-12-12 07:16:57                                            |
+----------------------------------------------------------------+
1 row in set

相关文章

  • MySQL 常用函数

    MySQL 常用函数 GROUP_CONCAT() 函数 CONCAT() 函数

  • 7、mysql常用函数

    7、mysql常用函数 7.1、IF函数 7.2、IFNULL函数 7.3、NULLIF 函数 7.4、CASE ...

  • Mysql常用函数

    ~~lpad(),rpad(): 字符长度填充: lpad(str,len,padstr) select LPAD...

  • mysql常用函数

    Conv(n,from_base,to_base) 对数字n进行进制转换并以字符串格式返回,from_base是原...

  • MySql常用函数

    本文引用燕归来http://www.zhoutao123.com/?p=99 字符串处理函数 字符串链接函数 可以...

  • MySQL常用函数

    字符串相关函数 1ASCII(String) 返回字符串第一个字母的ASCII码: SELECT ASCII('A...

  • MySQL 常用函数

    在 MySQL 数据库中,函数可以用在 SELECT 语句及其子句(例如 where、order by、havin...

  • MySQL 常用函数

    RAND() 函数 MySQL RAND() 函数可以被调用,产生一个在 0 和 1 之间的浮点数 当使用整数作为...

  • MySQL常用函数

    1.Concat(s1,s2...sn)函数:把传入的参数链接为一个字符串2.Insert(str,x,y,ins...

  • MySQL常用函数

    一、数学函数 ABS(x) 返回x的绝对值 BIN(x) 返回x的二进制(OCT返回八进制,HEX返回十六进制) ...

网友评论

      本文标题:MySQL 常用函数

      本文链接:https://www.haomeiwen.com/subject/ejjccxtx.html