简体   繁体   English

groovy SQL rows()方法

[英]groovy SQL rows() method

In my grails app a have next query: 在我的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")}"
"""

When im executing it manually in my db i get 3 rows. 当我在我的数据库中手动执行它时,我得到3行。 But when i'm doing this: 但是当我这样做时:

def queryResult = db.rows(query)

the size of queryResult is 1. Where is a problem? queryResult的大小为1。哪里有问题?

UPDADE. UPDADE。 I manually deleted form the db the row wich was found, and now method returns nothing, but exequting sql in phpmyadmin returns 2 rows 我从发现行所在的数据库中手动删除了数据库,现在方法什么也不返回,但是在phpmyadmin中用sql表示返回2行

you can try this: 您可以尝试以下方法:

   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
       }

    }

OK, i don't know why, but it works when query looks like this: 好的,我不知道为什么,但是当查询看起来像这样时它可以工作:

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")}"

This most likely has to do with Sql / GString interaction. 这很可能与Sql / GString交互有关。 If you perform def query = '''...'''.toString() or a def query = '''...''' as String before the call to db.rows(query) it will most likely solve your problem. 如果在调用db.rows(query)之前将def query = '''...'''.toString()def query = '''...''' as String的形式db.rows(query) ,则很可能会解决你的问题。 See this link for details and this link for a similar question. 请参见和链接的细节对于类似问题的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM