Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select records from today, this week, this month php mysql

I imagine this is pretty simple, but can't figure it out. I'm trying to make a few pages - one which will contain results selected from my mysql db's table for today, this week, and this month. The dates are entered when the record is created with date('Y-m-d H:i:s');. Here's what I have so far:

day where date>(date-(60*60*24))

 "SELECT * FROM jokes WHERE date>(date-(60*60*24)) ORDER BY score DESC" 

week where date>(date-(60*60*24*7))

 "SELECT * FROM jokes WHERE date>(date-(60*60*24*7)) ORDER BY score DESC" 

month (30 days) where date>(date-(60*60*24*30))

 "SELECT * FROM jokes WHERE date>(date-(60*60*24*30)) ORDER BY score DESC" 

Any ideas would be much appreciated. Thanks!

like image 745
Andrew Samuelsen Avatar asked Mar 13 '11 23:03

Andrew Samuelsen


People also ask

How do I get current week records in MySQL?

WEEK() function in MySQL is used to find week number for a given date. If the date is NULL, the WEEK() function will return NULL. Otherwise, it returns the value of week which ranges between 0 to 53. The date or datetime from which we want to extract the week.

Which function in MySQL can be used to get the date after 1 month?

Use the MONTH() function to retrieve a month from a date/datetime/timestamp column in MySQL. This function takes only one argument – either an expression which returns a date/datetime/ timestamp value or the name of a date/datetime/timestamp column.

How do I get current month data in SQL?

Using MONTH() and GETDATE() function to fetch current month In SQL, we use the GETDATE() method to retrieve the value of the current date which is today's date. Let us try executing a simple query statement using which we will retrieve the value of the current date using GETDATE() function.

Which of the following is the correct statement to display the current date in MySQL?

MySQL CURDATE() Function The CURDATE() function returns the current date.


1 Answers

Assuming your date column is an actual MySQL date column:

SELECT * FROM jokes WHERE date > DATE_SUB(NOW(), INTERVAL 1 DAY) ORDER BY score DESC;         SELECT * FROM jokes WHERE date > DATE_SUB(NOW(), INTERVAL 1 WEEK) ORDER BY score DESC; SELECT * FROM jokes WHERE date > DATE_SUB(NOW(), INTERVAL 1 MONTH) ORDER BY score DESC; 
like image 145
Nathan Ostgard Avatar answered Oct 09 '22 21:10

Nathan Ostgard