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
We use system function now() to get the latest datetime value, and INTERVAL clause to calculate a date 7 days in the past.
In order to get the date from the timestamp, you can use DATE() function from 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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With