HQL查询ORDER BY无法正常工作

时间:2016-10-17 18:34:53

标签: java mysql sql hibernate hql

我使用Spring Data JPA和Hibernate的HQL来创建数据库查询。

基本上,我有两张桌子。一个表格包含用户数据,另一个表格包含"广告系列"企业已上传。这些广告系列有视图。我想总结一下视图,按业务ID分组,然后按视图排序。

查询的工作原理是从数据库中提取正确的数据,它对视图进行求和,但不对它们进行排序。我一直在尝试各种方法来修复订单,但似乎无法找到解决方案。

My Spring Data JPA方法及其上面的查询:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not (Page.IsPostBack) Then
        InitCheckboxes()
    End If
End Sub

查询格式更好一点:

@Query("SELECT b, SUM(c.currentViews) AS views FROM BusinessUser b LEFT JOIN b.businessData.campaigns AS c"
                + " WHERE b.businessName LIKE %:keyword% GROUP BY c.businessUser ORDER BY views")
        public List<Object[]> lookupBusinessesByKeyword(@Param("keyword") String keyword);

我想要的例子:

SELECT
  *,
  SUM(c.currentViews) AS views
FROM BusinessUser b
  LEFT JOIN b.businessData.campaigns AS c
WHERE b.businessName LIKE % :keyword %
GROUP BY c.businessUser
ORDER BY views

现在,查询有效,但没有按正确的顺序排列视图。该表格按business_users table ----------------------- id name 1 businessOne 2 businessTwo 3 businessThree campaigns table ----------------------- id business_id title views 1 1 title 200 2 1 title 200 3 2 title 300 4 2 title 400 5 3 title 100 joined table ----------------------- businessId name SUM(views) 3 businessOne 700 1 businessTwo 400 2 businessThree 100 排序,而不是视图的总和,我不确定如何修复它。

修改 我目前的成绩:

businessId

0 个答案:

没有答案