Hi guys!
I’m working with a MySQL database and I can’t seem to execute a simple query with Union and In clauses.
A simpler example of what I’m trying to do is this:
SELECT `b` FROM `c`
WHERE (
`c`.`a` IN (
SELECT `c`.`a` FROM `a`, `c` WHERE `a`.`a` = `c`.`a`
UNION
SELECT `c`.`a` FROM `b`,`c` WHERE `b`.`a` = `c`.`a`
)
)
But, using the Query class of Yii2, what I get is actually:
SELECT `c`.`b` FROM `c`
WHERE (`c`.`a`) IN ((SELECT `b`.`a` FROM `b`, `c` WHERE b.a=c.a) UNION ( SELECT `a`.`a` FROM `a`, `c` WHERE a.a=c.a ))
And, because of the extra parenthesis around the subqueries in the union, I get a database error ('SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘UNION’).
Is there some way to solve this, using Query? Or should I write the sql statement "by hand"?
Any thought is appreciated, thanks!