在结果集中放置一个联合查询
Posted
技术标签:
【中文标题】在结果集中放置一个联合查询【英文标题】:Placing a Union query in the resultset 【发布时间】:2011-12-12 02:26:17 【问题描述】:任何人都可以建议是否有任何规则可以在 jdbc 结果集中放置联合查询? 我在数据库中有一个联合查询,我得到了输出......但是当我尝试在结果集中放置相同的联合查询时,
我收到一个错误:ASA 错误 -131:'unionselect' 附近的语法错误 在线...
谁能建议我哪里出错了...
jsp页面中查询如下.....
String QueryString =
"select CONVERT(VARCHAR(7),[startdatetime],111) AS [year-month],nm.nameLine1 as CompanyName, sum(datediff(hour, startdatetime, enddatetime)) as total"
+" from servicerequestworklog as srl"
+" inner join ServiceRequest sr on srl.ServiceRequestId = sr.ServiceRequestId"
+" inner join Name nm on(sr.clientCustomerId = nm.customerId and nm.nameTypeId = 'OFIC')"
+" where (startdatetime >= '08-01-2011 00:00:00.000' and enddatetime <= '10-31-2011 00:00:00.000')"
+" group by CompanyName, [year-month]"
+"union"
+"select CONVERT(VARCHAR(7),[startdatetime],111) AS [year-month], 'ZZTOTAL' as CompanyName, sum(datediff(hour, startdatetime, enddatetime)) as total"
+" from servicerequestworklog as srl"
+"inner join ServiceRequest sr on srl.ServiceRequestId = sr.ServiceRequestId"
+" where (startdatetime >= '08-01-2011 00:00:00.000' and enddatetime <= '10-31-2011 00:00:00.000')"
+" group by [year-month]"
+"order by CompanyName, [year-month]" ;
【问题讨论】:
【参考方案1】:您可以使用PreparedStatement
(参数化查询)来避免此类错误。我认为您必须在 union
和 select
单词之间添加空格。
【讨论】:
非常感谢!...我的问题解决了...正如您所料,这是由于 Union AND SELECT 之间的空格...以上是关于在结果集中放置一个联合查询的主要内容,如果未能解决你的问题,请参考以下文章