MySQL 查询如何将日期时间转换为日期?
mysqlmysqli database更新于 2024/10/22 10:52:17
您可以使用 MySQL 中的 CAST() 函数来实现这一点。语法如下 −
SELECT CAST(yourColumnName as Date) as anyVariableName from yourTableName;
为了理解上述语法,我们首先创建一个表。创建表的查询如下 −
mysql> create table ConvertDateTimeToDate -> ( -> ArrivalDatetime datetime -> ); Query OK, 0 rows affected (0.37 sec)
使用插入命令在表中插入日期时间。查询如下 −
mysql> insert into ConvertDateTimeToDate values(date_add(now(),interval -1 year));
Query OK, 1 row affected (0.19 sec)
mysql> insert into ConvertDateTimeToDate values('2017-11-21 13:10:20');
Query OK, 1 row affected (0.14 sec)
mysql> insert into ConvertDateTimeToDate values('2016-05-24 21:11:24');
Query OK, 1 row affected (0.26 sec)
mysql> insert into ConvertDateTimeToDate values('2012-04-30 04:05:50');
Query OK, 1 row affected (0.13 sec)
现在让我们使用 select 命令显示表中的所有记录。查询如下.
mysql> select *from ConvertDateTimeToDate;
以下是输出。
+---------------------+ | ArrivalDatetime | +---------------------+ | 2017-12-27 10:05:21 | | 2017-11-21 13:10:20 | | 2016-05-24 21:11:24 | | 2012-04-30 04:05:50 | +---------------------+ 4 rows in set (0.00 sec)
这是在 MySQL 中将日期时间转换为日期的查询。
mysql> select cast(ArrivalDatetime as Date) as Date from ConvertDateTimeToDate;
以下是输出。
+------------+ | Date | +------------+ | 2017-12-27 | | 2017-11-21 | | 2016-05-24 | | 2012-04-30 | +------------+ 4 rows in set (0.00 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

