一天一点MySQL复习——获取数据库系统时间变量赋值变量比较
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一天一点MySQL复习——获取数据库系统时间变量赋值变量比较相关的知识,希望对你有一定的参考价值。
一、SQL获取系统时间
1 mysql> select now() from dual; 2 +---------------------+ 3 | now() | 4 +---------------------+ 5 | 2016-05-24 08:34:12 | 6 +---------------------+ 7 1 row in set (0.00 sec) 8 mysql> select curdate() from dual; 9 +------------+ 10 | curdate() | 11 +------------+ 12 | 2016-05-24 | 13 +------------+ 14 1 row in set (0.01 sec) 15 mysql> select curtime() from dual; 16 +-----------+ 17 | curtime() | 18 +-----------+ 19 | 08:36:23 | 20 +-----------+ 21 1 row in set (0.00 sec)
二、变量赋值
赋值方式分为‘select‘与‘set‘
select 方式:
select @name:=value;
1 mysql> select @b:=4; 2 +-------+ 3 | @b:=4 | 4 +-------+ 5 | 4 | 6 +-------+ 7 1 row in set (0.00 sec) 8 9 mysql> select @b; 10 +------+ 11 | @b | 12 +------+ 13 | 4 | 14 +------+ 15 1 row in set (0.00 sec)
select row_name from table INTO @name;
1 mysql> select now() from dual INTO @test1; 2 Query OK, 1 row affected (0.00 sec) 3 4 mysql> select @test1; 5 +---------------------+ 6 | @test1 | 7 +---------------------+ 8 | 2016-05-24 08:48:21 | 9 +---------------------+ 10 1 row in set (0.00 sec)
set方式:
赋值:set @name:=value;
set @name=value;
1 mysql> set @c=1; 2 Query OK, 0 rows affected (0.00 sec) 3 4 mysql> select @c; 5 +------+ 6 | @c | 7 +------+ 8 | 1 | 9 +------+ 10 1 row in set (0.00 sec) 11 12 mysql> set @c:=2; 13 Query OK, 0 rows affected (0.00 sec) 14 15 mysql> select @c; 16 +------+ 17 | @c | 18 +------+ 19 | 2 | 20 +------+ 21 1 row in set (0.00 sec)
三、变量比较
1 mysql> select @time1,@time2; 2 +---------------------+---------------------+ 3 | @time1 | @time2 | 4 +---------------------+---------------------+ 5 | 2016-05-24 08:54:44 | 2016-05-24 08:27:57 | 6 +---------------------+---------------------+ 7 1 row in set (0.00 sec) 8 9 mysql> select now() from dual INTO @time1; 10 Query OK, 1 row affected (0.00 sec) 11 12 mysql> select now() from dual INTO @time2; 13 Query OK, 1 row affected (0.00 sec) 14 15 mysql> select @time1,@time2; 16 +---------------------+---------------------+ 17 | @time1 | @time2 | 18 +---------------------+---------------------+ 19 | 2016-05-24 08:58:23 | 2016-05-24 08:58:38 | 20 +---------------------+---------------------+ 21 1 row in set (0.00 sec) 22 23 mysql> select @time1 < @time2; 24 +-----------------+ 25 | @time1 < @time2 | 26 +-----------------+ 27 | 1 | 28 +-----------------+ 29 1 row in set (0.00 sec) 30 31 mysql> select @time1 > @time2; 32 +-----------------+ 33 | @time1 > @time2 | 34 +-----------------+ 35 | 0 | 36 +-----------------+ 37 1 row in set (0.00 sec)
以上是关于一天一点MySQL复习——获取数据库系统时间变量赋值变量比较的主要内容,如果未能解决你的问题,请参考以下文章