用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 送芒果的祝福语简短
18 送给学长毕业祝福语简短