用MySQL抓取当前日期和前一天在哪里?

您可以CURDATE()使用DATE_SUB()和INTERVAL 1 DAY来获取MySQL的当前日期和前一天。语法如下:

SELECT DATE_SUB(CURDATE(),INTERVAL 1 DAY);

使用date_sub()获取日期和日期的语法如下。

SELECT *FROM yourTableName WHERE yourColumnName = CURDATE() OR yourColumnName = DATE_SUB(CURDATE(),INTERVAL 1 DAY);

为了理解上述语法,让我们创建一个表。创建表的查询如下:

mysql> create table ProductDemo

   -> (

   -> Id int NOT NULL AUTO_INCREMENT,

   -> ProductName varchar(20),

   -> ProductOfferDate datetime,

   -> PRIMARY KEY(Id)

   -> );

使用insert命令在表中插入一些记录。在这里,我们添加了产品和产品报价日期。查询如下:

mysql> insert into ProductDemo(ProductName,ProductOfferDate) values('Product-11','2017-05-21');

mysql> insert into ProductDemo(ProductName,ProductOfferDate) values('Product-22','2019-01-15');

mysql> insert into ProductDemo(ProductName,ProductOfferDate) values('Product-21','2019-01-14');

mysql> insert into ProductDemo(ProductName,ProductOfferDate) values('Product-91','2018-10-23');

mysql> insert into ProductDemo(ProductName,ProductOfferDate) values('Product-133','2019-01-24');

使用select语句显示表中的所有记录。查询如下:

mysql> select *from ProductDemo;

以下是输出:

+----+-------------+---------------------+

| Id | ProductName | ProductOfferDate    |

+----+-------------+---------------------+

| 1 | Product-11 | 2017-05-21 00:00:00 |

| 2 | Product-22 | 2019-01-15 00:00:00 |

| 3 | Product-21 | 2019-01-14 00:00:00 |

| 4 | Product-91 | 2018-10-23 00:00:00 |

| 5 | Product-133 | 2019-01-24 00:00:00 |

+----+-------------+---------------------+

5 rows in set (0.00 sec)

以下是获取当前日期和前一天的产品的查询:

mysql> select *from ProductDemo

   -> where ProductOfferDate = CURDATE() OR ProductOfferDate = date_sub(curdate(),interval 1 day);

以下是输出:

+----+-------------+---------------------+

| Id | ProductName | ProductOfferDate    |

+----+-------------+---------------------+

|  2 | Product-22  | 2019-01-15 00:00:00 |

|  3 | Product-21  | 2019-01-14 00:00:00 |

+----+-------------+---------------------+

2 rows in set (0.00 sec)

以上是 用MySQL抓取当前日期和前一天在哪里? 的全部内容, 来源链接: utcz.com/z/358061.html

回到顶部