Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grab where CURDATE() and the day before with MySQL

Tags:

mysql

offers.date = CURDATE() 

I what I currently have.

It grabs offers for today, but I also would like to grab the orders for yesterday.

How can I do this, without specifying the yesterday's date?

like image 869
Karem Avatar asked Nov 26 '12 19:11

Karem


People also ask

What does Curdate () do in SQL?

The CURDATE() function returns the current date. Note: The date is returned as "YYYY-MM-DD" (string) or as YYYYMMDD (numeric). Note: This function equals the CURRENT_DATE() function.

How can I get specific date records in MySQL?

You can use DATE() from MySQL to select records with a particular date. The syntax is as follows. SELECT *from yourTableName WHERE DATE(yourDateColumnName)='anyDate'; To understand the above syntax, let us first create a table.

How can I get yesterday date record in MySQL?

To get yesterday's date, you need to subtract one day from today's date. Use CURDATE() to get today's date. In MySQL, you can subtract any date interval using the DATE_SUB() function. Here, since you need to subtract one day, you use DATE_SUB(CURDATE(), INTERVAL 1 DAY) to get yesterday's date.

How can I get date between two dates in MySQL?

To count the difference between dates in MySQL, use the DATEDIFF(enddate, startdate) function. The difference between startdate and enddate is expressed in days. In this case, the enddate is arrival and the startdate is departure .


1 Answers

To use CURDATE minus or plus a interval (e.g. yesterday) you can use the DATE_ADD function

SELECT DATE_ADD(CURDATE(), INTERVAL -1 DAY);

So, in your case you use it like this:

WHERE offers.date = CURDATE() OR offers.date = DATE_ADD(CURDATE(), INTERVAL -1 DAY)

Optionally you can also use the DATE_SUB() function and instead of a negative interval use the same interval but positive.

Thus, DATE_ADD(CURDATE(), INTERVAL -1 DAY) would become DATE_SUB(CURDATE(), INTERVAL 1 DAY)

like image 101
edwardmp Avatar answered Oct 26 '22 16:10

edwardmp