Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you find results that occurred in the past week?

I have a books table with a returned_date column. I'd like to see the results for all of the books with a returned date that occurred in the past week.

Any thoughts? I tried doing some date math, but Postgres wasn't happy with my attempt.

like image 387
Huuuze Avatar asked Jan 04 '12 19:01

Huuuze


People also ask

How can I get the latest record in database based on datetime?

If you are using ->latest()->get() then it will get records base on created_at, if we get records base on created_at then how about order by id desc both work as same. if you need by other column datetime then you can use ->orderBy('columnName','desc/asc')->get() otherwise you can use latest() function.

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.


1 Answers

You want to use interval and current_date:

select * from books where returned_date > current_date - interval '7 days' 

This would return data from the past week including today.

Here's more on working with dates in Postgres.

like image 188
Eric Avatar answered Oct 12 '22 20:10

Eric