MySQL 查询按列显示当前日期来获取当前日期记录


为了实现此目标,下面是使用 DATE(NOW()) - 的语法

select *from yourTableName where DATE(yourColumnName)=DATE(NOW());

我们先创建一个表格

mysql> create table DemoTable706 (
   UserId varchar(100),
   UserName varchar(100),
   UserSignupDate datetime
);
Query OK, 0 rows affected (0.57 sec)

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

mysql> insert into DemoTable706 values('[email protected]','John','2019-01-31 12:45:22');
Query OK, 1 row affected (0.24 sec)
mysql> insert into DemoTable706 values('[email protected]','Chris','2019-07-22 10:05:02');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable706 values('[email protected]','Robert','2019-06-22 11:25:22');
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable706 values('[email protected]','David','2019-07-22 00:00:02');
Query OK, 1 row affected (0.12 sec)

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

mysql> select *from DemoTable706;

这将生成以下输出

+--------------------+----------+---------------------+
| UserId             | UserName | UserSignupDate      |
+--------------------+----------+---------------------+
| [email protected]    | John     | 2019-01-31 12:45:22 |
| [email protected] | Chris    | 2019-07-22 10:05:02 |
| [email protected] | Robert   | 2019-06-22 11:25:22 |
| [email protected]   | David    | 2019-07-22 00:00:02 |
+--------------------+----------+---------------------+
4 rows in set (0.00 sec)

以下是获取列中一个显示当前日期的记录的查询

mysql> select *from DemoTable706 where DATE(UserSignupDate)=DATE(NOW());

这将生成以下输出

+--------------------+----------+---------------------+
| UserId             | UserName | UserSignupDate      |
+--------------------+----------+---------------------+
| [email protected] | Chris    | 2019-07-22 10:05:02 |
| [email protected]   | David    | 2019-07-22 00:00:02 |
+--------------------+----------+---------------------+
2 rows in set (0.00 sec)

更新于: 21-8-2019

181 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始
广告