如果条件满足,则在 MySQL 中选择一列,从当前日期和当前日期 + 1 中获取记录

mysqlmysqli database更新于 2023/12/30 18:21:00

让我们首先获取当前日期 −

mysql> select curdate();

这将产生以下输出 −

+------------+
| curdate()  |
+------------+
| 2019-12-15 |
+------------+
1 row in set (0.00 sec)

首先我们创建一个表 −

mysql> create table DemoTable1956
   (
   ProductId int,
   ProductName varchar(20),
   CustomerName varchar(20),
   ShippingDate date
   );
Query OK, 0 rows affected (0.00 sec)

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

mysql> insert into DemoTable1956 values(101,'Product-1','Sam','2019-10-11');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1956 values(102,'Product-2','Carol','2018-12-01');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1956 values(103,'Product-3','David','2019-12-15');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1956 values(104,'Product-4','Robert','2019-12-16');
Query OK, 1 row affected (0.00 sec)

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

mysql> select * from DemoTable1956;

这将产生以下输出 −

+-----------+-------------+--------------+--------------+
| ProductId | ProductName | CustomerName | ShippingDate |
+-----------+-------------+--------------+--------------+
|       101 | Product-1   | Sam          | 2019-10-11   |
|       102 | Product-2   | Carol        | 2018-12-01   |
|       103 | Product-3   | David        | 2019-12-15   |
|       104 | Product-4   | Robert       | 2019-12-16   |
+-----------+-------------+--------------+--------------+
4 rows in set (0.00 sec)

以下查询用于选择一列并获取当前日期和下一个日期的记录 −

mysql> select CustomerName from DemoTable1956 where ShippingDate IN(CURDATE(),CURDATE()+interval 1 Day);

这将产生以下输出 −

+--------------+
| CustomerName |
+--------------+
| David        |
| Robert       |
+--------------+
2 rows in set (0.00 sec)

相关文章