栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

SQL的HQL版本与group by联接

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SQL的HQL版本与group by联接

在HQL中,您可以尝试一下:

select band.name, max(vote.totalVotes)from Band band     join band.votes votegroup by band.nameorder by max(vote.totalVotes) desc

这是假设之间存在一个一对多的关联

Band
Votes
(实际上,提供对象模型HQL和/或标准API,因为要查询的对象模型工作时非常有用)。

以防万一,这是文档的相关部分:

[14.12。group

by子句](http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-
grouping)

返回聚合值的查询可以按返回的类或组件的任何属性进行分组:

select cat.color, sum(cat.weight), count(cat)from Cat catgroup by cat.colorselect foo.id, avg(name), max(name)from Foo foo join foo.names namegroup by foo.id

还可以使用Have子句。

select cat.color, sum(cat.weight), count(cat)from Cat catgroup by cat.colorhaving cat.color in (eg.Color.TABBY, eg.Color.BLACK)

如果基础数据库支持SQL函数和聚合函数,则它们的Have和order by子句中允许使用它们(即,MySQL中不支持)。

select catfrom Cat cat    join cat.kittens kittengroup by cat.id, cat.name, cat.other, cat.propertieshaving avg(kitten.weight) > 100order by count(kitten) asc, sum(kitten.weight) desc

group by子句和order
by子句都不能包含算术表达式。Hibernate当前也不会扩展分组的实体,因此,如果未聚合cat的所有属性,则无法按cat编写group。您必须明确列出所有未聚合的属性。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/412932.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号