【问题标题】:How can I get whole row with distinct column in hibernate using hql?如何使用 hql 在休眠中获得具有不同列的整行?
【发布时间】:2017-11-17 16:57:36
【问题描述】:

测试表名称

Id      Name      Profession
1        a         Engg
2        b         Engg
3        c         Doctor
4        d         Doctor

期望的输出:

 Id      Name      Profession
 1        a         Engg
 4        d         Doctor

我想获取包含上表所示的独特职业的行。我试过了

hql-

“从测试中选择不同的职业”

它正在返回字符串专业。如何获得整行?

【问题讨论】:

  • 如何为此选择 id,因为在 Engg 中您选择了 1,但在 Doctor 中您选择了 4
  • Id 可以是随机的。我只需要一排 engg 和一排医生。

标签: hibernate hql distinct


【解决方案1】:

DISTINCT 将只返回具有唯一值的列。

而不是:

select distinct profession from test

使用下面的 hql 查询:

select * from test group by profession

【讨论】:

  • 谢谢它的工作。但我总是得到a和c,我的意思是这两个职业的第一要素。有什么方法可以让我们获取随机值而不是总是先获取?
  • @DevenderYadav:我不这么认为。
  • 这样就行了!
猜你喜欢
  • 2012-07-07
  • 1970-01-01
  • 1970-01-01
  • 2012-02-19
  • 1970-01-01
  • 1970-01-01
  • 2011-02-03
  • 2018-03-19
  • 2011-08-14
相关资源
最近更新 更多