2

以下は私の2つのクラスです

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%使用しています。削除するcountcountDistinct、個別の作成者のリストを取得した場合ですが、必要なのは作成者とドキュメントの数です。したがって、countまたはcountDistinct句をこの基準に追加すると、longの配列が取得されます。[2,3,4]のように私が欲しいのは[[author1,2]、[author2,3] ...]どうすればこれを達成できますか?すでに見たGrails:多くのテーブルでの投影?Grails基準の予測-行数GrailsgroupPropertyおよびorderを取得します。使い方?、しかし、答えのどれも私の問題を解決していないようです!

4

3 に答える 3

1

ページ付けのあるプロジェクションには、プロジェクションブロックの最後のフィールドのみを返すというバグがあります。現在のgrails2.1.5では、このバグが修正されています。報告されたバグは次のとおりですhttp://jira.grails.org/browse/GRAILS-9644

于 2013-05-18T17:23:29.977 に答える
0

countBy*を使用できませんでした-

    SharedDocuments.countByAuthorAndReceiver(user, receiver)
于 2012-04-19T05:28:14.113 に答える
0

クエリで説明したとおりの結果が必要な場合は、作成者のIDまたは電子メールなどの他の特定のプロパティが必要です。


def sharedDocumentsInstanceList = SharedDocuments.createCriteria().list(params){
    eq("receiver.id",session.uid)  
    projections{
        groupProperty "author"
        count "id",'mycount'

    }
     order('mycount','desc')
    maxResults(params.max)
}
于 2012-04-19T08:06:11.597 に答える