NHibernate HQL子查询问题
我的(简化的)域模型包含称为客户和产品之间的所有权的多对多关系。我想编写一个 NHibernate HQL 查询来统计拥有每种产品的客户数量(在特定条件下)。请注意,客户可以多次拥有给定产品,但这只能算作对该产品的一次“投票”。
执行此操作的 SQL 非常简单:
select ProductID, count(*)
from (
select distinct CustomerID, ProductID
from Ownership
where ...
) tbl
group by ProductID
不幸的是,HQL 不支持 from
子句中的子查询。如何用 HQL 表达这个查询?
My (simplified) domain model contains a many-to-many relationship called Ownership between Customer and Product. I would like to write an NHibernate HQL query that tallies the number of customers who own each product (under certain criteria). Note that a customer can own a given product multiple times, but this should only count as one "vote" for that product.
The SQL for this is easy enough:
select ProductID, count(*)
from (
select distinct CustomerID, ProductID
from Ownership
where ...
) tbl
group by ProductID
Unfortunately, HQL does not support subqueries in the from
clause. How can I express this query in HQL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢贾森·麦克利
Credit to Jason Meckley