Skip to content
Advertisement

Combine two python mysql queries with a condition

I want to grab some specific rows of two tables before and after a certain time, this is my code:

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?

Advertisement

Answer

UNION doesn’t permits any comparison between columns, so where tb2x.id=tb2y.id after a UNION is an error

https://en.wikipedia.org/wiki/Set_operations_(SQL)#UNION_operator

To compare two different sections of a table you have to JOIN itself:

Another thing is collect all rows in one table before and after a certain time. You need to use UNION of two sub-query table, each of which is a filter of the input table:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement