下面是我的两个类
class Users {
String emailAddress
String password
// String filename
String firstName
String lastName
Date dateCreated
Date lastUpdated
}和
class SharedDocuments {
Users author
Users receiver
Documents file
static constraints = {
}
}我想运行一个类似于这个查询的查询,本质上我想获得所有用户的列表以及他们所创作的文档的数量
SELECT author_id, COUNT(SharedDocuments.id )FROM SharedDocuments
INNER JOIN users ON author_id = users.id
GROUP BY author_id这是我到目前为止所拥有的
def sharedDocumentsInstanceList = SharedDocuments.createCriteria().list(params){
createAlias("author","a")
eq("receiver.id",session.uid)
projections{
groupProperty "author"
count "id",'mycount'
}
order('mycount','desc')
maxResults(params.max)
}我有这90%的工作,如果我摆脱了count或countDistinct,我得到了不同的作者列表,但我想要的是作者和文档的计数。因此,当我将count或countDistinct子句添加到此条件中时,我只得到long数组!!比如2,3,4我想要的是[author1,2,Author2,3...]我已经看过Grails: Projection on many tables?,Grails criteria projections - get rows count,Grails groupProperty and order. How it works?,但似乎没有一个答案能解决我的问题!
发布于 2013-05-19 01:23:30
使用分页的Projections有一个bug,它只返回projections块中的最后一个字段。当前的grails 2.1.5已经修复了这个错误。下面是报告的错误http://jira.grails.org/browse/GRAILS-9644
发布于 2012-04-19 13:28:14
你就不能直接用countBy* - like
SharedDocuments.countByAuthorAndReceiver(user, receiver)发布于 2012-04-19 16:06:12
如果您想要查询中描述的结果,那么您需要作者的id或任何其他特定属性,如电子邮件。
def sharedDocumentsInstanceList = SharedDocuments.createCriteria().list(params){
eq("receiver.id",session.uid)
projections{
groupProperty "author"
count "id",'mycount'
}
order('mycount','desc')
maxResults(params.max)
}https://stackoverflow.com/questions/10222083
复制相似问题