本文介绍了如何在HQL中执行联合SQL语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用HQL(Hibernate查询语言)在两个表之间创建一个联合。这个SQL脚本在我的SQL服务器上工作正常:

  SELECT COUNT(DISTINCT linkedin_id)as test,school_name 
FROM
(SELECT * FROM alum_education
UNION
SELECT * FROM alum_connection_educations)AS UNIONS where school_name ='some string'

问题是,当我尝试在grails中运行它时:

  def countOfEdu = AlumEducation.executeQuery(select count(distinct linkedinId)as countOfEdu,schoolName as SchoolName from(SELECT * FROM alumEducation UNION SELECT * FROM alumConnectionEducations)AS UNIONS where schoolName ='some string')



我得到这个错误:

  org.hibernate.hql.ast.QuerySyntaxException:意外的标记:(在第1行第83列[select count(distinct linkedinId)as countOfEdu,schoolName as SchoolName from(SELECT * FROM alumEducation UNION SELECT * FROM alumConnectionEducations)AS UNIONS where schoo lName ='Duquesne University'] 

如何在grails中运行上述SQL语句?



感谢
jason

解决方案

HQL不支持联合。 Hibernate的JIRA中存在一个,该问题自2005年开放。


I'm trying to create a Union between two tables, using HQL (Hibernate Query Language). This SQL script works fine on my SQL server:

SELECT COUNT(DISTINCT linkedin_id) as test, school_name
FROM
(SELECT * FROM alum_education
 UNION
 SELECT * FROM alum_connection_educations) AS UNIONS where school_name='some string'

the problem is, when i try to run it in grails like this:

     def countOfEdu = AlumEducation.executeQuery("select count (distinct linkedinId ) as countOfEdu, schoolName as SchoolName from (SELECT * FROM alumEducation UNION SELECT * FROM alumConnectionEducations) AS UNIONS where schoolName='some string'" )

i get this error:

  org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ( near line 1, column 83 [select count(distinct linkedinId )  as countOfEdu, schoolName as SchoolName  from (SELECT * FROM alumEducation UNION SELECT * FROM alumConnectionEducations) AS UNIONS where schoolName='Duquesne University']

How can I run the above SQL statement in grails?

thanksjason

解决方案

Unions are not supported by HQL. There is an issue in Hibernate's JIRA that is open since 2005.

这篇关于如何在HQL中执行联合SQL语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 16:37