I want to grab some specific rows of two tables before and after a certain time, this is my code:
mysql_query = """
select tb1x.id
from table1 as tb1x
join table2 as tb2x ON tb2x.id = tb1x.cid
where tb1x.created > CURDATE()
UNION ALL
(select tb1y.id
from table1 as tb1y
join table2 as tb2y ON tb2y.id = tb1y.cid
where tb1y.created < CURDATE()
)
where tb2x.id=tb2y.id
ORDER BY tb1y.created DESC;"""
It gives an error and indicates that UNION is not used properly.
and I guess the problem could be here: AND tb2x.id=tb2y.id
Could you please help where is the problem or if it's an smart way to compare two different sections of a table?
UNIIONand not something like a JOIN?(..). Due to order by clause, MySQL is unable to interpret properly.AND tb2x.id=tb2y.id?(select .... Where..) union (select ... Where...) order by..