用MySQL抓取当前日期和前一天在哪里?
您可以CURDATE()使用DATE_SUB()和INTERVAL1DAY来获取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)
热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿