Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL select only where Timestamp is from last 10 days

I want to limit my query to results that have been entered in the last 10 days. The TIMESTAMP column is called Date. How do I do it?

$result = mysql_query("SELECT * FROM Posts WHERE (City = '$city2') ORDER by Comments DESC LIMIT 5");

Thanks

like image 360
lisovaccaro Avatar asked Aug 03 '11 19:08

lisovaccaro


People also ask

How do I get last 7 days data in SQL query?

We use system function now() to get the latest datetime value, and INTERVAL clause to calculate a date 7 days in the past.

How can I get only date from timestamp?

In order to get the date from the timestamp, you can use DATE() function from MySQL.

How do I get last week records in MySQL?

Here's the SQL query to get last week's data in MySQL. In the above SQL query, we use WEEK() function to get Week number of order_date column. We select only those records whose week number is 1 less than the week number of today's date, obtained used NOW() function.

How do I select a date from a timestamp in SQL?

In MySQL, use the DATE() function to retrieve the date from a datetime or timestamp value. This function takes only one argument – either an expression which returns a date/datetime/ timestamp value or the name of a timestamp/datetime column.


1 Answers

SELECT *
FROM Comments
WHERE (City = '$city2') AND (`Date` > DATE_SUB(now(), INTERVAL 10 DAY));

Note: calling a column 'Date' is poor practice, since it's a reserved word.

like image 139
Marc B Avatar answered Sep 22 '22 13:09

Marc B