如何获得不同的结果,但在选择中返回另一列?
我想根据属性获得不同的结果,但在选择中返回 id,因为我将在子查询中使用它。
例如,
(List<Article>) session.createQuery("from Article a where a.id in (select distinct a2.title from article a2 where a2.body = :body");
setString("body", "")
.list();
关键部分是子查询,我想返回id,而不是a2.title属性。 这可以做到吗?
(表 Article 有重复项,所以我只想返回其中任何一篇,只要 body = "" 就没关系)。
I want to get a distinct result based on a property, but return the id in the select because I will be using it in a subquery.
e.g.
(List<Article>) session.createQuery("from Article a where a.id in (select distinct a2.title from article a2 where a2.body = :body");
setString("body", "")
.list();
The key section is the subquery, I want to return the id, not the a2.title property.
Can this be done?
(the table Article has duplicates, so I just want to return any one of them it doesn't matter as long as the body = "").
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不关心返回哪一行,则可以将子查询更改为:
这将从
article
中返回body
匹配的最低id
。If you don't care which row is returned, you could change your subquery to:
This would return the lowest
id
fromarticle
wherebody
matches.这是什么 SQL 服务器?
您可以要求只返回一行,而不是使用不同的,然后您只需要选择您实际需要的字段。
在 PL\SQL (Oracle) 中,您可以使用 rownum:
在其他 SQL 中,您可以使用 limit:
read: http://www.petefreitag.com/item/451.cfm
what SQL server is this?
Instead of using distinct you could ask for just one line to return, and then you'd only need to select the field(s) you actually need.
In PL\SQL (Oracle) you can use rownum:
In other SQL you could use limit:
read: http://www.petefreitag.com/item/451.cfm