# 函数

TODO:待补充

MySQL 函数:https://www.runoob.com/mysql/mysql-functions.html

例:

mysql> select "Hello";          # select 可当做 echo 命令使用
+-------+
| Hello |
+-------+
| Hello |
+-------+
1 row in set (0.00 sec)

mysql> select 1 + 3;            # 可进行 C 语言的算术运算
+-------+
| 1 + 3 |
+-------+
|     4 |
+-------+
1 row in set (0.00 sec)

mysql> select 1 - 3;
+-------+
| 1 - 3 |
+-------+
|    -2 |
+-------+
1 row in set (0.00 sec)

mysql> select 10/3;
+--------+
| 10/3   |
+--------+
| 3.3333 |
+--------+
1 row in set (0.00 sec)

mysql> select 10%3;
+------+
| 10%3 |
+------+
|    1 |
+------+
1 row in set (0.04 sec)

mysql> select 10/3 > 3;             # 布尔值用 1、0 表示 true、false
+----------+
| 10/3 > 3 |
+----------+
|        1 |
+----------+
1 row in set (0.00 sec)

mysql> select round(3.5);           # 可以用 round() 函数取整(四舍五入)
+------------+
| round(3.5) |
+------------+
|          4 |
+------------+
1 row in set (0.00 sec)
mysql> select now();                # 返回当前时间
+---------------------+
| now()               |
+---------------------+
| 2020-04-30 11:26:21 |
+---------------------+
1 row in set (0.00 sec)

mysql> select date_format(now(), '%Y-%m-%d %H:%i:%s');    # 返回指定格式的时间
+---------------------------------------+
| date_format(now(), '%Y-%m-%d %H:%i:%s') |
+---------------------------------------+
| 2020-04-30 11:41:37                     |
+---------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT to_days('1-1-1');             # 返回从公元 0 年开始的天数
+------------------+
| to_days('1-1-1') |
+------------------+
|              366 |
+------------------+
1 row in set (0.00 sec)

mysql> SELECT to_days(now()) - to_days('1-1-1');
+-----------------------------------+
| to_days(now()) - to_days('1-1-1') |
+-----------------------------------+
|                            737544 |
+-----------------------------------+
1 row in set (0.00 sec)

mysql> SELECT UNIX_TIMESTAMP("2020-04-30 11:41:37");    # 返回时间戳
+---------------------------------------+
| UNIX_TIMESTAMP("2020-04-30 11:41:37") |
+---------------------------------------+
|                            1588218097 |
+---------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT round((UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP("2020-04-30 11:41:37")) / 60);  # 计算两个时间相差的分钟数
+-----------------------------------------------------------------------------+
| round((UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP("2020-04-30 11:41:37")) / 60) |
+-----------------------------------------------------------------------------+
|                                                                           8 |
+-----------------------------------------------------------------------------+
1 row in set (0.00 sec)

SELECT COUNT(*) FROM student; # 计算返回的数据的 row 数 select version(); # 查看版本号