Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
groovy SQL rows()方法_Sql_Grails_Groovy - Fatal编程技术网

groovy SQL rows()方法

groovy SQL rows()方法,sql,grails,groovy,Sql,Grails,Groovy,在my 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

在my 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行

您可以尝试以下操作:

   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
       }

    }

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

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
/
GString
交互有关。如果在调用
db.rows(query)
之前执行
def query=''''''''''''.toString()
或将
def query=''''''''''''''''作为字符串执行
。有关详细信息,请参阅链接;有关类似问题,请参阅链接