使用SQL中的用户定义变量获取最长考试日期

要使用用户定义的变量获取最长考试日期,代码如下-

select date(max(yourColumnName )) into @yourVariableName  from yourTableName;

要了解上述语法,让我们首先创建一个表-

mysql> create table DemoTable2001
(
   ExamDate date
);

使用插入命令在表中插入一些记录-

mysql> insert into DemoTable2001 values('2019-01-10');
mysql> insert into DemoTable2001 values('2018-12-31');
mysql> insert into DemoTable2001 values('2018-11-18');
mysql> insert into DemoTable2001 values('2019-07-25');

使用select语句显示表中的所有记录-

mysql> select * from DemoTable2001;

这将产生以下输出-

+------------+
| ExamDate   |
+------------+
| 2019-01-10 |
| 2018-12-31 |
| 2018-11-18 |
| 2019-07-25 |
+------------+
4 rows in set (0.00 sec)

这是获取最长日期的查询。我们将首先创建一个用户定义的变量-

mysql> set @comingExamDate:=null;
mysql> select date(max(ExamDate)) into @comingExamDate from DemoTable2001;
mysql> select @comingExamDate;

这将产生以下输出-

+------------------+
| @commingExamDate |
+------------------+
| 2019-07-25       |
+------------------+
1 row in set (0.00 sec)