mysql - Get data where MAX(date) < X -
i have 2 tables one-to-many relationship.
table1 id name email table2 id table1_id date
i need data table1 where
:
max(date) table2 < "2016-01-01"
this doesn't work. max considered "invalid" in clause. did :
select table1.name, table1.email, tmp.maxdate table1 join ( select max(date) maxdate, table1_id table2 group table1_id ) tmp on tmp.table1_id = table1.id tmp.maxdate < "2016-01-01" , (other conditions)
so works. think performance going awful - explain
shows table2
being read, , table grow lot.
any idea on how otherwise, or how improve current query performances ?
try:
select table1.name, table1.email, tmp.maxdate table1 inner join ( select max(date) maxdate, table1_id table2 group table1_id having maxdate > "2016-01-01" ) tmp on tmp.table1_id = table1.id , (other conditions)
before, bringing table2 , join table1. knock off without maxdate > "2016-01-01" , join on table1.
Comments
Post a Comment