首页 文章

使用条件和/或投影(子查询)将SQL转换为休眠查询

提问于
浏览
0

任何人都可以帮助将此SQL查询转换为HQL(使用条件和投影)以获得结果 Set<topic> topics .

SELECT t.id, t.title, COUNT(p.id) as count, MAX(p.created_on) as `latest`
FROM topics t
JOIN posts p
  ON t.id = p.topic_id
GROUP BY t.id
ORDER BY count ASC;

我的问题是Hibernate正在获取连接到 topic 表的一列(连接表是 users )的所有数据 - 并且查询需要花费很多时间和不必要的选择和连接 .
试图使用预测,但不能 .
在此先感谢您的帮助!

1 回答

  • 0

    从您的代码中,

    SELECT t.id, t.title, COUNT(p.id) as count, MAX(p.created_on) as `latest`
    FROM topics t
    JOIN posts p
      ON t.id = p.topic_id
    GROUP BY t.id
    ORDER BY count ASC;
    

    您可以删除 JOIN 并将其转换为 WHERE 条件 . 希望它会拯救你 .

    使用以下代码

    SELECT t.id, t.title, COUNT(p.id) as count, MAX(p.created_on) as `latest`
    FROM topics t, posts p
    WHERE t.id = p.topic_id
    GROUP BY t.id
    ORDER BY count ASC;
    

相关问题