如何使用HQL检索具有最低投票数的最佳文章的排序列表?

发布于 2024-09-04 02:46:53 字数 450 浏览 3 评论 0原文

我的 grails 应用程序有一个 Vote 域类,其中包含 article_idnote 等属性,

我想通过 HQL 查询 Vote > 域类,以便检索至少有 10 票的 5 篇评分最高的文章。

我尝试过:

SELECT v.article_id, avg(v.note), count(*) FROM vote v where count(*) >= 10  group by v.article_id order by avg(v.note) desc limit 5;

但不幸的是插入 where count(*) >= 10 会引发错误。

我怎样才能以简单的方式做到这一点?

感谢您的帮助。

I have a Vote domain class from my grails application containing properties like article_id and note

I want to HQL query the Vote domain class in order to retrieve the 5 best rated articles having at least 10 votes.

I tried :

SELECT v.article_id, avg(v.note), count(*) FROM vote v where count(*) >= 10  group by v.article_id order by avg(v.note) desc limit 5;

But unfortunately the insertion of where count(*) >= 10 throws an error.

How can I do that in a simple way?

Thank you for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

A君 2024-09-11 02:46:53

由于无法测试它,我不愿意尝试猜测最终查询会是什么样子,但是 HAVING COUNT(*) >= 10 可能值得一看。

http://www.sqlcommands.net/sql+having/

Not being able to test it, I'm reluctant to try to guess how the final query would look like, but HAVING COUNT(*) >= 10 might be something to look at.

http://www.sqlcommands.net/sql+having/

递刀给你 2024-09-11 02:46:53

尝试一下:

SELECT v.article_id, avg(v.note),
FROM vote v 
GROUP BY v.article_id
HAVING count(*) >= 10
OREDER BY avg(v.note) desc limit 5;

Try that:

SELECT v.article_id, avg(v.note),
FROM vote v 
GROUP BY v.article_id
HAVING count(*) >= 10
OREDER BY avg(v.note) desc limit 5;
燃情 2024-09-11 02:46:53

以下不起作用吗?

SELECT v.article_id, avg(v.note), count(*) as count 
FROM vote v 
where count >= 10  
group by v.article_id 
order by avg(v.note) desc limit 5;

稍后编辑:试试这个

SELECT v.article_id, avg(v.note) 
from vote v 
group by v.article_id 
having count(*) >= 10
order by avg(v.note) desc limit 5;

doesn't the following work ?

SELECT v.article_id, avg(v.note), count(*) as count 
FROM vote v 
where count >= 10  
group by v.article_id 
order by avg(v.note) desc limit 5;

Later edit: Try this

SELECT v.article_id, avg(v.note) 
from vote v 
group by v.article_id 
having count(*) >= 10
order by avg(v.note) desc limit 5;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文