[英]How i can convert the following sql query to HQL query
I have the following query.How i can convert the following query to HQL query. 我有以下查询。如何将以下查询转换为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 |
+--------+------------+-------+
I want to get the following result 我想得到以下结果
+--------+----------+-------+
| 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;
I think this should work (not tested): 我认为这应该工作(未经测试):
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
)
EDIT : Keep in mind that if a type
has the same price
for more than one variety
, than all of those rows will be selected with this query. 编辑 :请记住,如果一个type
具有多个variety
的相同price
,则此查询将选择所有这些行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.