0

在我的 grails 应用程序中有下一个查询:

def query = """select
                       u.id, u.birth_date, u.gender, uht.data
                    from
                       user_hra_test as uht
                       left join user as u on u.id = uht.user_id
                       left join client as c on c.id = u.client_id
                    where
                       c.id in (${clients*.id.join(',')}) and
                       uht.`date` between "${start.format("yyyy-MM-dd")}" and "${end.format("yyyy-MM-dd")}"
"""

当我在我的数据库中手动执行它时,我得到 3 行。但是当我这样做时:

def queryResult = db.rows(query)

queryResult 的大小为 1。问题出在哪里?

更新。我从数据库中手动删除了找到的行,现在方法什么也不返回,但是在 phpmyadmin 中执行 sql 返回 2 行

4

3 回答 3

0

这很可能与Sql/GString交互有关。如果您在调用之前执行def query = '''...'''.toString()def query = '''...''' as String调用db.rows(query)它很可能会解决您的问题。有关详细信息,请参阅链接,有关类似问题,请参阅此链接。

于 2012-09-08T01:06:30.293 回答
0

你可以试试这个:

   def dataSource

   def nameMethod() {

       def sql = Sql.newInstance(dataSource)

       def query = def query = """select
                   u.id, u.birth_date, u.gender, uht.data
                from
                   user_hra_test as uht
                   left join user as u on u.id = uht.user_id
                   left join client as c on c.id = u.client_id
                where
                   c.id in (${clients*.id.join(',')}) and
                   uht.`date` between "${start.format("yyyy-MM-dd")}" and   "${end.format("yyyy-MM-dd")}"
               """

       sql.eachRow( query ) { 
           println it //Do whatever you need with each row
       }

    }
于 2012-09-06T17:24:41.050 回答
0

好的,我不知道为什么,但是当查询如下所示时它可以工作:

select
                       u.id, u.birth_date, u.gender, uht.data
                    from
                       user_hra_test as uht
                       left join user as u on u.id = uht.user_id
                       left join client as c on c.id = u.client_id
                    where
                       c.id in ("""+clients*.id.join(',')+""") and
                       uht.`date` between "${start.format("yyyy-MM-dd")}" and "${end.format("yyyy-MM-dd")}"
于 2012-09-06T18:05:26.133 回答