Showing the count of rows in a MySQL table, where the logged events are exactly x year and y month old

Mango :

I have a quite simple database which autologs events and dates. I've recently tried looking at how many events took place exactly x years and y month ago. I know this can be acommplished by manually looking up the date and making a where year(time) = x and month(time)=y, but is there an option to do this with timediff or datesub somehow?

GMB :

how many events took place exactly x years and y month ago

I think that you want:

select *
from mytable 
where 
    mydate >= current_date - interval X year - interval Y month
    and mydate < current_date - interval X year - interval Y month + interval 1 day

This gives you the events that occured within the entire day X year and Y months ago (which you need to replace with the relevant integer values), using MySQL date arithmetics.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=395105&siteId=1