2012-09-06 19 views
0

在我的Grails应用程式中有一个查询:常规SQL行()方法

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

当IM手动执行它在我的分贝我得到3行。但是当我这样做时:

def queryResult = db.rows(query) 

queryResult的大小是1.哪里出了问题?

UPDADE。我手动删除形式分贝行至极被发现,现在方法返回不算什么,但在phpMyAdmin exequting SQL返回2行

回答

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 
     } 

    } 
+0

没有工作,请参阅更新 –

0

OK,我不知道为什么,但它工作时查询看起来是这样的:

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

这最有可能与Sql/GString互动做。如果您在致电db.rows(query)之前执行def query = '''...'''.toString()def query = '''...''' as String,它很可能会解决您的问题。详情请见this链接,this也有类似问题的链接。