1.查询当前日期时间:函数有now(),localtime(),current_timestamp(),sysdate()。
mysql> select now(),localtime(),current_timestamp(),sysdate();
+---------------------+---------------------+---------------------+---------------------+
| now() | localtime() | current_timestamp() | sysdate() |
+---------------------+---------------------+---------------------+---------------------+
| 2015-04-16 09:29:18 | 2015-04-16 09:29:18 | 2015-04-16 09:29:18 | 2015-04-16 09:29:18 |
+---------------------+---------------------+---------------------+---------------------+
1 row in set (0.00 sec)
但是now()与sysdate()有点差异的,一个语句中now()的值是不变的,而sysdate()是动态获取的,例如
mysql> select now(),sleep(2),now();
+---------------------+----------+---------------------+
| now() | sleep(2) | now() |
+---------------------+----------+---------------------+
| 2015-04-16 09:34:30 | 0 | 2015-04-16 09:34:30 |
+---------------------+----------+---------------------+
1 row in set (2.00sec)
mysql> select sysdate(),sleep(2),sysdate();
+---------------------+----------+---------------------+
| sysdate() | sleep(2) | sysdate() |
+---------------------+----------+---------------------+
| 2015-04-16 09:35:15 | 0 | 2015-04-16 09:35:17 |
+---------------------+----------+---------------------+
1 row in set (2.00sec)
-----有此结果可以看出,一般在生成环境中最好使用now(),当然也可以使用sysdate-is-now参数改变sysdate()的行为和now()一样------
2.获取当前日期,curdate()=current_date()=current_date
mysql> select curdate(),current_date(),current_date;
+------------+----------------+--------------+
| curdate() | current_date() | current_date |
+------------+----------------+--------------+
| 2015-04-16 | 2015-04-16 | 2015-04-16 |
+------------+----------------+--------------+
1 row in set (0.00 sec)
3.获取当前时间,curtime()=current_time()=current_time
mysql> select curtime(),current_time(),current_time;
+-----------+----------------+--------------+
| curtime() | current_time() | current_time |
+-----------+----------------+--------------+
| 09:42:17 | 09:42:17 | 09:42:17 |
+-------