我如何将以下sql查询转换为HQL查询

时间:2013-06-18 06:31:32

标签: mysql hibernate hql

我有以下查询。如何将以下查询转换为HQL查询。

+--------+------------+-------+
| type   | variety    | price |
+--------+------------+-------+
| apple  | gala       |  2.79 | 
| apple  | fuji       |  0.24 | 
| apple  | limbertwig |  2.87 | 
| orange | valencia   |  3.59 | 
| orange | navel      |  9.36 | 
| pear   | bradford   |  6.05 | 
| pear   | bartlett   |  2.14 | 
| cherry | bing       |  2.55 | 
| cherry | chelan     |  6.33 | 
+--------+------------+-------+

我想得到以下结果

+--------+----------+-------+
| type   | variety  | price |
+--------+----------+-------+
| apple  | fuji     |  0.24 | 
| orange | valencia |  3.59 | 
| pear   | bartlett |  2.14 | 
| cherry | bing     |  2.55 | 
+--------+----------+-------+

 select f.type, f.variety, f.price
from (
   select type, min(price) as minprice
   from fruits group by type
) as x inner join fruits as f on f.type = x.type and f.price = x.minprice;

1 个答案:

答案 0 :(得分:0)

我认为这应该有效(未经测试):

select f.type, f.variety, f.price
from fruits f
where f.price = (
  select min(f2.price)
  from fruits f2
  where f2.type = f.type
)

编辑:请注意,如果type对于多个price具有相同的variety,则会选择所有这些行这个查询。