我想在评级表中打印具有外键Movie_id的Movies表中的标题。在评级表中我们必须根据movie_id发生得到前10个结果。因为HQL中不允许限制所以 setMaxResults 使用
Query q=session.createQuery("select Title from Movies as M Inner Join ( SELECT Movie_id, COUNT(*) FROM Rating GROUP BY Movie_id ORDER BY COUNT(*) DESC LIMIT 10 ) as R ON M.Movie_id=R.Movie_id").setMaxResults(10);
例外情况是:
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ( near line 1, column 59 [select Title from com.rahul.model.Movies as M Inner Join ( SELECT Movie_id, COUNT(*) FROM com.rahul.model.Rating GROUP BY Movie_id ORDER BY COUNT(*) DESC LIMIT 10 ) as R ON M.Movie_id=R.Movie_id]
既然HQL不支持子查询如何实现呢?
答案 0 :(得分:2)
您可以将此查询编写为JOIN
/ GROUP BY
:
select m.Title
from Movies m Inner Join
Rating r
on m.Movie_id = r.Movie_id
group by m.Movie_Id, m.Title
order by count(*) desc
limit 10;
答案 1 :(得分:0)
已解决
SELECT CountryCode, IsOfficial, COUNT(Language) as LangCount FROM countrylanguage WHERE IsOfficial = 'T' GROUP BY CountryCode, IsOfficial HAVING COUNT(Language) > 1 UNION ALL SELECT CountryCode, IsOfficial, COUNT(Language) as LangCount FROM countrylanguage WHERE IsOfficial = 'F' GROUP BY CountryCode, IsOfficial HAVING COUNT(Language) > 2 ORDER BY CountryCode