“联合"处或附近的 SQL 语法错误 [英] SQL syntax error at or near 'union'

查看:81
本文介绍了“联合"处或附近的 SQL 语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小查询,还有一个联合可以在它旁边放置另一个小查询.但是,联合中有一个语法错误.

I have a small query, and a union to put another small query next to it. However, the union has a syntax error in it.

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'

推荐答案

我明白出了什么问题.您必须在查询结束时下订单,并且只能在最后下订单.它给了我一个错误,因为它认为查询已经完成.

I see what was wrong. You have to place the order by at the end of the query, and only at the end. It gave me an error because it thought the query had eneded.

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 语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆