Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL syntax error while using Limit with UnionAll in MySQL #454

Closed
Sanjay85500 opened this issue Mar 16, 2021 · 5 comments
Closed

SQL syntax error while using Limit with UnionAll in MySQL #454

Sanjay85500 opened this issue Mar 16, 2021 · 5 comments
Projects

Comments

@Sanjay85500
Copy link

I'm using UnionAll with Limit and using MySQL compiler to get SqlResult, while I'm executing the command it is throwing SQL syntax error.
Below is the code
var query = new Query("table1").UnionAll(q => q.From("table2")).Limit(10); var sql = new MySqlCompiler().Compile(query);
sql result is SELECT * FROM table1 LIMIT 10 UNION ALL SELECT * FROM table2

but the syntax should be (SELECT * FROM table1 LIMIT 10) UNION ALL (SELECT * FROM table2)

@ahmad-moussawi
Copy link
Contributor

For ref: https://stackoverflow.com/questions/1415328/combining-union-and-limit-operations-in-mysql-query

as a workaround for now, you can wrap the original query in a subquery, it's not identical but same result

var query = new Query().From(
    new Query("table1").Limit(10).As("sub")
).UnionAll(q => q.From("table2"));

Check
https://sqlkata.com/playground/mysql?code=var%20query%20%3D%20new%20Query().From(new%20Query(%22table1%22).Limit(10).As(%22sub%22)).UnionAll(q%20%3D%3E%20q.From(%22table2%22))%3B

@Sanjay85500
Copy link
Author

Hi, @ahmad-moussawi thanks for the above comment it works for me, according to this docs it should give a proper syntax maybe there is an issue with SqlKata.

@ahmad-moussawi
Copy link
Contributor

Yes indeed

@ahmad-moussawi
Copy link
Contributor

Why did you close it :-)?
It's a valid case and should be fixed, I will reopen it

@ahmad-moussawi ahmad-moussawi added this to To do in Tasks Jul 16, 2021
@ahmad-moussawi
Copy link
Contributor

I am rethinking about this, and concluding that this should be the expected result, since if we are instructing the QueryBuilder to build a wrong query, it should not correct it itself.
So I will close this one, but if you have another opinion, I am open for suggestions

Tasks automation moved this from To do to Done Oct 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Tasks
Done
Development

No branches or pull requests

2 participants