mysql - How do I retrieve data for the previous month in SQL -
i want data dates between 2015-05-01 , 2015-06-01 using sql. please me query.
the query used is:
select *,count(id) multiple_visitors table1 id=123 , (date(server_time) between (curdate() - interval 31 day) , curdate()) group user_id having count(id)>1
you can month() , year():
where month(server_time) = month(curdate() - interval 1 month) , year(server_time) = year(curdate() - interval 1 month) however, recommend more complex expression:
where server_time >= date_sub(date_sub(curdate(), interval - day(curdate()) + 1 day), interval 1 month) , server_time < date_sub(curdate(), interval - day(curdate()) + 1 day) the advantage there no functions on server_time, database engine can use index, if appropriate.
as note: expression date_sub(curdate(), interval - day(curdate()) + 1 day) gets midnight on first day of month.
Comments
Post a Comment