Can HQL Select on the result set of another query?(HQL 可以在另一个查询的结果集上选择吗?)
问题描述
HQL 可以在另一个查询的结果集上选择吗?例如:
Can HQL Select on the result set of another query? For example:
SELECT COUNT(*) FROM (SELECT * FROM Table)
我可以在 SQL 中执行此操作,但是当我在 HQL 中进行上述尝试时,它只是显示语法错误意外的标记:(第 1 行附近,第 22 列 ..."
I can do it in SQL but when I tried like above in HQL, it just showed me syntax error "unexpected token: ( near line 1, column 22 ..."
推荐答案
HQL 确实支持 子查询,但是它们只能出现在 select 或 where 子句中.您提供的示例最好在 HQL 中编写为直接语句.例如:
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)
如果查询涉及比 (select * from Table) 更复杂的语句,我建议将此逻辑放入视图中,然后根据该视图创建实体.
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.
对于支持子查询的数据库,Hibernate 支持子查询在查询中.子查询必须用括号括起来(通常是SQL 聚合函数调用).甚至相关的子查询(在外部查询中引用别名的子查询)是允许的.
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.
示例
from DomesticCat as cat
where cat.name not in (
select name.nickName from Name as name
)
这篇关于HQL 可以在另一个查询的结果集上选择吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:HQL 可以在另一个查询的结果集上选择吗?
基础教程推荐
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- Struts2 URL 无法访问 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
