用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)
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短