hql不同的顺序

时间:2012-07-03 10:17:50

标签: sql-order-by hql distinct

您好我需要选择最新子对象排序的不同对象,我该怎么做? 我试图从子对象中选择父对象:

SELECT DISTINCT r.forumTheme FROM ForumResponse r ORDER BY r.responseId DESC

我得到了

ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

当我尝试这个时:

SELECT DISTINCT r.forumTheme,r.responseId FROM ForumResponse r ORDER BY r.responseId DESC

结果不是很明显:forumTheme可以出现多次不同的responseId

有什么解决方案吗?

1 个答案:

答案 0 :(得分:1)

对于给定的论坛主题,您有几个responseIds。那么它们应该如何排序呢? [6, 3, 2]大于或小于[5, 4, 2]

这就是为什么你不能这样做的原因。

您应该执行以下查询:

select r.forumTheme, max(r.responseId) from ForumResponse r 
group by r.forumTheme
order by max(r.responseId)