Anthony
Anthony

Reputation: 36008

How to count number of records by 'in' clause in GORM

I want to run a sql query similar to:

SELECT count(*) from WORDS where wordId in (4,5,6)

How can I write this in Gorm? I know grails has countBy* but I can't pass multiple values like [4,5,6] into it.

should I just use execute?

Upvotes: 0

Views: 1900

Answers (2)

dmahapatro
dmahapatro

Reputation: 50285

You can also use rowCount in the projections which counts the number of rows of the result. Note id as Long

Words.createCriteria().get {
  'in'('id', [4,5,6]*.toLong()) //or [4L, 5L, 6L]
  projections {
    rowCount()
  }
}

Upvotes: 1

user800014
user800014

Reputation:

You can use projections.

Words.createCriteria().get {
  'in'('id', [4,5,6])
  projections {
    count('id')
  }
}

Upvotes: 0

Related Questions