UNION query is not using parenthesis
See original GitHub issueHello, I’m doing the following query (on mysql):
s1 = SiteLog.select(SiteLog.time).order_by(SiteLog.time.desc())
s2 = SubLog.select(SubLog.time).order_by(SubLog.time.desc())
final = (s1 | s2)
and the sql result is
SELECT `t1`.`time` FROM `site_log` AS `t1` ORDER BY `t1`.`time` DESC UNION SELECT `t2`.`time` FROM `sub_log` AS `t2` ORDER BY `t2`.`time` DESC
This lacks the parenthesis around each query so the mysql server ends up throwing a syntax error:
peewee.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT `t2`.`time` FROM `sub_log` AS `t2` ORDER BY `t2`.`time` DESC' at line 1")
What am I doing wrong?
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
why I have to add parentheses to make union work
Without extra parentheses, LIMIT is only allowed once at the end of the query and applies to the result of the UNION. To...
Read more >UNION syntax missing required parentheses - MySQL Bugs
Description: MySQL queries produced by the LINQ Concat() or Union() operators are missing parentheses around the individual select ...
Read more >UNION - MariaDB Knowledge Base
Individual SELECTs can contain their own ORDER BY and LIMIT clauses. In this case, the individual queries need to be wrapped between parentheses....
Read more >3 Operators, Functions, Expressions, Conditions
You can use parentheses in an expression to override operator precedence. ... UNION. All rows selected by either query. UNION ALL.
Read more >SQL UNION overview, usage and examples - SQLShack
The output is a combination of Union and Union All operators using parenthesis. . How to use SQL Union with the queries that...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
See also #2014 – mysql and mariadb are a pile of inconsistency when it comes to parsing sql.
Bug for MySQL (opened in 2007) was finally fixed for MySQL 8.0 release: https://bugs.mysql.com/bug.php?id=25734
MariaDB thought they fixed it for 10.4: https://jira.mariadb.org/browse/MDEV-11953
But they missed some scenarios, including using compound select queries inside an IN expression (e.g.). I’ve opened a ticket, so we’ll see what happens: https://jira.mariadb.org/browse/MDEV-20606
Thanks! This all was pretty quick!