0

我有以下查询。如何将以下查询转换为 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;
4

1 回答 1

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
)

编辑:请记住,如果 a与多个type相同,则此查询将选择所有这些行。pricevariety

于 2013-06-18T06:56:52.940 回答