Search code examples
mysqlgrailsgrails-orm

Running a query in grails with two 'like' conditions with an OR in between


I am trying to run a grails query which is as follows

select * from table where col1 like 'abc%' or col2 like 'abc%'

When I run DomainClass.executeQuery it fails with the error table_name not mapped. I dont know how to write it in createCriteria. Can you please provide some guidance?


Solution

  • If you want to use criteria:

    def c = Table.createCriteria()
    def results = c.list {
        or {
            like("col1", "abc%")
            like("col2", "abc%")
        }
    }
    

    http://grails.github.io/grails-doc/3.0.x/ref/Domain%20Classes/createCriteria.html