HQL可以选择另一个查询的结果集吗? [英] Can HQL Select on the result set of another query?

查看:223
本文介绍了HQL可以选择另一个查询的结果集吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HQL可以选择另一个查询的结果集吗?

例如:

$ p $ SELECT COUNT(*)FROM(SELECT * FROM Table)

我可以在SQL中完成它,但是当我在HQL中尝试如上所示时,它只是显示语法错误意外的标记:(靠近第1行,第22列...

//docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/queryhql.html#queryhql-subqueriesrel =noreferrer>子查询,但它们只能出现在select或者where子句,你提供的例子最好在HQL中写成一个直接的语句,例如:

  select count *)从表t(其中table是实体名称)

如果查询涉及更复杂的语句比(select * from Table),我建议把这个逻辑放到一个视图中,然后根据这个视图创建一个实体。


用于数据库支持子查询,Hibernate支持查询中的子查询
。子查询必须被括号括起来(通常是
的SQL聚合函数调用)。甚至相关的子查询
(引用外部查询中的别名的子查询)也是允许的。


/ strong>

  from DomesticCat as cat 
其中cat.name不在(
select name.nickName从名称作为名称


Can HQL Select on the result set of another query?
For example:

SELECT COUNT(*) FROM (SELECT * FROM Table)


I can do it in SQL but when I tried like above in HQL but the it just show me syntax error "unexpected token: ( near line 1, column 22 ..."

解决方案

HQL does support subqueries, however they can only occur in the select or the where clause. The example you provide would best be wrote as a straight statement in HQL. For example:

select count(*) from table t  (where table is the entity name)

If the query involves a more complicated statement than (select * from Table), I would recommend putting this logic into a view and then creating an entity based off of this view.

For databases that support subselects, Hibernate supports subqueries within queries. A subquery must be surrounded by parentheses (often by an SQL aggregate function call). Even correlated subqueries (subqueries that refer to an alias in the outer query) are allowed.

Example

from DomesticCat as cat
where cat.name not in (
    select name.nickName from Name as name
)

这篇关于HQL可以选择另一个查询的结果集吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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