mysql 經常使用函數

floor(x) 返回小於x的最大整數值mysql

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值的整數部分.sql

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


rand():返回0到1之間的隨機數.ide

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之間的隨機整數.spa

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開始計數.orm


mysql> create table t18 (
    -> email char(20)
    -> );
Query OK, 0 rows affected (0.02 sec)
mysql> insert into t18 values ("abc@163.com"),("12356@163.com");
Query OK, 2 rows affected (0.02 sec)
Records: 2  Duplicates: 0  Warnings: 0
mysql> select * from t18;
+---------------+
| email         |
+---------------+
| abc@163.com   |
| 12356@163.com |
+---------------+
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語法截取:it

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:時間戳函數.io

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)
相關文章
相關標籤/搜索