3
I am searching a table in my database that needs to return all the data that was created in the current month (I will run a cron on the last day of the month at 23:00). I have in my table a field created_at
keeping the date entered on the basis of the record.
Here’s what I did:
SELECT *
FROM minha_tabela
WHERE created_at >= date_add(last_day(date_sub(curdate(), interval 1 month)), interval 1 day)
AND created_at <= last_day(curdate());
It’s working, only I’d like to know if it’s better if I leave anyway or if there’s some better way to do it, thinking about performance and maintenance.
A way that at least already facilitates maintenance, and from what I saw neither improves nor worsens the performance. Valew
– Marcelo Diniz
I cannot say in mysql, but in sql server there is a loss of performance if the field uses indexes, as they are disregarded when the
where
is calculated by means of an expression (month()
andyear()
in that case)– gmsantos