Re: sql query issue
Posted by: Todd Farmer
Date: October 12, 2012 10:39AM

Hi Suleman,

It's not entirely clear to me what you are trying to achieve. Your example suggests that you only want to return the two rows that have a date_time_from that's *exactly* a week before execution time. Yet your query uses greater than or equal to a week before execution time.

Assuming you want just the two records from 01.10.2012, you could use:

SELECT * FROM messages_stats_analysis_ota WHERE date_time_from = DATE_SUB(CURDATE(), INTERVAL 7 DAY);

Your data doesn't show any time component, despite the column name. If there is actually a time component, the above query likely won't match, because it will require a match to the exact second. If you are meaning to match just on the date component, you would need to use:


SELECT * FROM messages_stats_analysis_ota
WHERE date_time_from >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)
AND date_time_from < DATE_SUB(CURDATE(), INTERVAL 6 DAY);

Note that there is an equivalent statement that will compare only the date and ignore the time:

SELECT * FROM messages_stats_analysis_ota
WHERE DATE(date_time_from) = DATE_SUB(CURDATE(), INTERVAL 7 DAY);

You don't want to use this, though, because MySQL will be unable to use any indexes on date_time_from due to the use of the function.

Hope that helps!

--
Todd Farmer
MySQL @ Oracle
http://www.oracle.com/mysql/

Options: ReplyQuote


Subject
Written By
Posted
October 12, 2012 08:54AM
Re: sql query issue
October 12, 2012 10:39AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.