简体   繁体   English

在休眠状态下对计算列进行过滤? (有)

[英]Filtering on calculated columns in hibernate? (HAVING)

Say I have a table with two columns, X and Y. I would like to run the following query via hibernate: 假设我有一个包含X和Y两列的表。我想通过hibernate运行以下查询:

SELECT X*X + Y*Y AS Distance, POINTS.* from POINTS 
WHERE X > 0 
AND Y > 0
HAVING Distance < 50

How do I achieve it via hibernate? 如何通过休眠实现它? Can you provide a code sample? 您可以提供代码示例吗?

Edit - This question seems heavily related. 编辑 - 这个问题似乎密切相关。 The only reason I'm not closing this one as a duplicate, is because it provides a much simpler use case (no GROUP BY). 我不关闭此副本的唯一原因是因为它提供了更为简单的用例(没有GROUP BY)。

In most dialects of SQL, you cannot use the 'display label' or the name in 'AS name' to refer to an expression inside the body of the query - much to the chagrin of people. 在大多数SQL方言中,您不能使用“显示标签”或“ AS名称”中的名称来引用查询主体内的表达式,这非常让人感到烦恼。 However, you can use the expression in the WHERE clause. 但是,您可以在WHERE子句中使用该表达式。

SELECT X*X + Y*Y AS DistanceSquared, POINTS.* from POINTS 
 WHERE X > 0 
   AND Y > 0
   AND (X * X + Y * Y) < 50;

The HAVING clause is associated with aggregates and comparisons on aggregates: HAVING子句与聚合和聚合比较相关联:

SELECT name, COUNT(*)
  FROM SomeWhere
 GROUP BY Name
HAVING COUNT(*) > 1;

The conditions in a HAVING clause must (should) involve at least one 'direct' aggregate; HAVING子句中的条件必须(应该)涉及至少一个“直接”聚合; the other term in the condition might be a constant (as shown) or another direct aggregate, or a sub-query, possibly involving 'indirect' aggregates. 条件中的另一项可能是常量(如图所示),另一个直接聚合或可能涉及“间接”聚合的子查询。 (AFAIK, I've invented the 'direct/indirect' terminology for this answer - you probably won't be able to search for the expression usefully.) (AFAIK,我已经为此答案发明了“直接/间接”术语-您可能无法有效地搜索该表达式。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM