MySQL Forums
Forum List  »  General

Re: How returns Last Month?
Posted by: Peter Brawley
Date: April 04, 2020 02:40PM

In a Where clause you don't need date_format() ...

WHERE fixtime between curdate() - interval 1 month and last_day(curdate() - interval 1 month) limit 1;

But in a Where clause you can simplify it a lot more ...

set @lastmo = if( month(curdate())=1, 12, month(curdate()-1 ) );

select ... where month(fixtime) = @lastmo;

... or optimising for a presumed index on fixtime ...

set @lastmo = left( curdate()-interval 1 month, 7);

select ... where left(fixtime,7) = @lastmo



Edited 1 time(s). Last edit at 04/04/2020 02:44PM by Peter Brawley.

Options: ReplyQuote


Subject
Written By
Posted
Re: How returns Last Month?
April 04, 2020 02:40PM


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.