mysql 常用函數

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)



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章