JPQL / SQL:如何从单列上具有分组依据的表中选择*?
我想选择表的每一列,但希望在行的单个属性上具有不同的值(示例中的城市)。我不需要额外的列,例如计数或任何其他列,只需要有限数量的结果,而且似乎不可能直接在 JPQL 查询中限制结果。
原始表:
ID | Name | City
---------------------------
1 | John | NY
2 | Maria | LA
3 | John | LA
4 | Albert | NY
如果我在城市上做不同的事情,想要的结果:
ID | Name | City
---------------------------
1 | John | NY
2 | Maria | LA
最好的方法是什么?感谢您的帮助。
I would like to select every column of a table, but want to have distinct values on a single attribute of my rows (City in the example). I don't want extra columns like counts or anything, just a limited number of results, and it seems like it is not possible to directly LIMIT results in a JPQL query.
Original table:
ID | Name | City
---------------------------
1 | John | NY
2 | Maria | LA
3 | John | LA
4 | Albert | NY
Wanted result, if I do the distinct on the City:
ID | Name | City
---------------------------
1 | John | NY
2 | Maria | LA
What is the best way to do that? Thank you for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 JPQL 中,您可以执行以下操作:
这会返回:
In JPQL, you could do something like this:
This returns:
不知道 JPQL,但 SQL 是:
Dunno about JPQL, but the SQL would be: