floor(x) 返回小于x的最大整数值

mysql> select * from t13;
+------+--------+----------+
| id   | name   | fraction |
+------+--------+----------+
|    1 | 李四   | 65.5     |
+------+--------+----------+
1 row in set (0.00 sec)
mysql> select fraction  from t13;
+----------+
| fraction |
+----------+
| 65.5     |
+----------+
1 row in set (0.00 sec)

#floor截取fraction值的整数部分.

mysql> select floor(fraction)  from t13;
+-----------------+
| floor(fraction) |
+-----------------+
|              65 |
+-----------------+
1 row in set (0.00 sec)


rand():返回0到1之间的随机数.

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


返回5-10之间的随机数.

mysql> select rand()*5+5;
+-------------------+
| rand()*5+5        |
+-------------------+
| 6.166499062596354 |
+-------------------+
1 row in set (0.00 sec)
mysql> select rand()*5+5;
+-------------------+
| rand()*5+5        |
+-------------------+
| 7.710514615951585 |
+-------------------+
1 row in set (0.00 sec)

配合floor可以得到5-10之间的随机整数.

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

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

position获取指定字符串所在列的位置,从1开始计数.


mysql> create table t18 (
    -> email char(20)
    -> );
Query OK, 0 rows affected (0.02 sec)
mysql> insert into t18 values ("[email protected]"),("[email protected]");
Query OK, 2 rows affected (0.02 sec)
Records: 2  Duplicates: 0  Warnings: 0
mysql> select * from t18;
+---------------+
| email         |
+---------------+
| [email protected]   |
| [email protected] |
+---------------+
2 rows in set (0.00 sec)

position使用的语法:

mysql> select position('@' in email) from t18;
+------------------------+
| position('@' in email) |
+------------------------+
|                      4 |
|                      6 |
+------------------------+
2 rows in set (0.00 sec)

left语法截取:

mysql> select left(email,position('@' in email)) from t18;
+------------------------------------+
| left(email,position('@' in email)) |
+------------------------------------+
| abc@                               |
| 12356@                             |
+------------------------------------+
2 rows in set (0.01 sec)

mysql> select left(email,position('@' in email)-1) from t18;
+--------------------------------------+
| left(email,position('@' in email)-1) |
+--------------------------------------+
| abc                                  |
| 12356                                |
+--------------------------------------+
2 rows in set (0.00 sec)

date_format:时间戳函数.

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2019-02-15 23:09:31 |
+---------------------+
1 row in set (0.00 sec)
mysql> select date_format(now(),'%Y%m');
+---------------------------+
| date_format(now(),'%Y%m') |
+---------------------------+
| 201902                    |
+---------------------------+
1 row in set (0.00 sec)
mysql> select date_format(now(),'%Y%m%d');
+-----------------------------+
| date_format(now(),'%Y%m%d') |
+-----------------------------+
| 20190215                    |
+-----------------------------+
1 row in set (0.00 sec)