Friday, 15 March 2013

php - MySQL query by exact date -


I am querying from my database and actually blog posts (more specifically, every year on this date ) By trying to get, the following code posts should do according to what I've found, but it's getting all posts instead of last year.

 Select  $ sq = "* where` user_id` = '$ user_id' and POST_DATE & gt; CURDATE () - INTERVAL 1 year order POST_DATE by DESC"; $ Result = $ dbc-> Query ($ sql);  

What can I do for a year? I could write a loop to increase the year if I could actually find out the query for one year.

After

If it is WordPress, then post_data is a DATETIME field. So just a year ago means '2014-03-16 09:03:27'. I doubt what you want me to think you want last year on this day

Try this:

  POST_DATE & Gt; = CURDATE () - INTERVAL 1 year and after Date & lt; Kurt () - Interval 1 year + interval 1 day  

The position of that pair takes all the posts, starting from midnight on that day a year ago, but does not include midnight on the following day is. MySQL implements them with a efficient index range scan.

If you are looking for posts for any year on this year's day, then you need this type of expression.

  MONTH (CURDATE ()) = MONTH (post_date) and defofforth (curate ()) = DAYOFMONTH (post_date)  

This is my first suggestion as fast as It will not be because it does not use an index; Besides, it will give strange results on leaps like '2016-02-29'.


No comments:

Post a Comment