Nhibernate HQL 函数

本文介绍了一系列HQL查询示例,包括统计数量、求平均值、取最大/最小值及合计等操作。此外还展示了如何使用HQL进行计算、类型转换以及使用字符、数学和日期函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面是一组HQL的例子,代码中的s都代表Nhibernate的Session

统计个数
1.用select方法统计(去除了重复)
s.CreateQuery("select count(distinct a.id) from Animal a").UniqueResult()

s.CreateQuery("select count(*) from Animal").UniqueResult();
2.用where 方法统计
s.CreateQuery("select count(a.id) from Animal a having count(a.id)>1").UniqueResult();

统计平均数
1.用select方法统计
s.CreateQuery("select avg(a.BodyWeight) from Animal a").UniqueResult();
2.用where 方法统计
s.CreateQuery("select avg(a.BodyWeight) from Animal a having avg(a.BodyWeight)>0").UniqueResult();

取 最大/最小值/合计 的例子和上面的例子类似,只是avg可以换成 max,min,sum

返回一个指定的类
假设你有一个类:SummaryItem即统计类,你要返回一个列表的统计结果,你可以
s.CreateQuery("select distinct new SummaryItem(a.Description, sum(BodyWeight)) from Animal a").List<SummaryItem>()

在HQL中进行计算
s.CreateQuery("select a.Id, sum(BodyWeight)/avg(BodyWeight) from Animal a group by a.Id having sum(BodyWeight)>0").List()

很难得HQL还有SubString Locate Trim Length Bit_length Coalesce Upper等字符计算功能,你可以

SubString
hql = "select substring(a.Description, 3) from Animal a";
或者
hql = "from Animal a where substring(a.Description, 2, 3) = 'bcd'";

hql = "from Animal a where substring(a.Description, 2, 3) = ?";
s.CreateQuery(hql).SetParameter(0, "bcd").UniqueResult();

Locate
hql = "select locate('bc', a.Description, 2) from Animal a";

hql = "from Animal a where locate('bc', a.Description) = 2";

Trim
hql = "select trim(a.Description) from Animal a where a.Description=' def'";

hql = "from Animal a where trim(a.Description) = 'abc'";

hql = "from Animal a where trim(leading from a.Description) = 'def'";

hql = "from Animal a where trim(both from a.Description) = 'abc'";

Length
hql = "select length(a.Description) from Animal a where a.Description = '1234'";
hql = "from Animal a where length(a.Description) = 5";

Bit_length
hql = "select bit_length(a.Description) from Animal a";
hql = "from Animal a where bit_length(a.Description) = 24";

Coalesce
hql = "select coalesce(h.NickName, h.Name.First, h.Name.Last) from Human h";
hql = "from Human h where coalesce(h.NickName, h.Name.First, h.Name.Last) = 'max'";

Upper,Lower例子类似

类型转换的例子

Cast
hql = "select cast(a.BodyWeight as Double) from Animal a";
hql = "select cast(7+123-5*a.BodyWeight as Double) from Animal a";
hql = "from Animal a where cast(a.BodyWeight as string) like '1.%'";

str
hql = "select str(a.BodyWeight) from Animal a";
hql = "from Animal a where str(123) = '123'";

很难得HQL还有Nullif等判断功能,你可以

Nullif
hql1 = "select nullif(h.NickName, '1e1') from Human h";
hql2 = "from Human h where not(nullif(h.NickName, '1e1') is null)";

很难得HQL还有Abs Mod Sqrt等数学函数功能,你可以

Abs
hql = "select abs(a.BodyWeight*-1) from Animal a";
hql = "from Animal a where abs(a.BodyWeight*-1)>0";
hql = "select abs(a.BodyWeight*-1) from Animal a group by abs(a.BodyWeight*-1) having abs(a.BodyWeight*-1)>0";

Mod
hql = "select mod(cast(a.BodyWeight as int), 3) from Animal a";
hql = "from Animal a where mod(20, 3) = 2";
hql = "from Animal a where mod(cast(a.BodyWeight as int), 4)=0";

和时间相关的例子

hql = "select second(current_timestamp()), minute(current_timestamp()), hour(current_timestamp()) from Animal";
hql = "select day(h.Birthdate), month(h.Birthdate), year(h.Birthdate) from Human h";  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值