我有一个小查询,并有一个联合将另一个小查询放在它旁边。但是,union中有语法错误。
Select <column1>
,<column2>
From <Table1>
<Some joins in there>
Where <conditions>
group by <column2>
order by <column2>
union
select <column2>
,<column3>
,<column4>
From <Table2>
<Some more joins here>
Where <conditions>
group by <column2>
order by <column2>
这是我收到的错误
ERROR: Syntax error at or near 'union'
最佳答案
我知道怎么了。您必须在查询的结尾处放置order by,并且只能在结尾处放置order by。它给了我一个错误,因为它认为查询已经完成了。
Select <column1>
,<column2>
,<aggregate column3>
From <Table1>
<Some joins in there>
Where <conditions>
group by <column2>, <column1>
union
select <column2>
,<column3>
,<aggregate column4>
From <Table2>
<Some more joins here>
Where <conditions>
group by <column2>, <column3>
order by <column2>
这就成功了。
关于sql - 'union'或附近的SQL语法错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24020078/