使用createCriteria进行Grails子查询

时间:2019-01-18 20:51:55

标签: sql grails nested-query createcriteria

我有如下所示的sql查询

select transf, count(fname)  from peak_info where fname  in (select peakfile from pe_result where conid = 'GO:0006007' and fdr > 0.05) group by transf;

我想在grails中实现的

创建标准。当前,我先运行括号中的SQL查询,然后运行如下外部查询:

 def test2 = PeResult.createCriteria()
        def ptest=test2.list {
            eq("conid",conid.toString())
            gt("fdr","0.05")
            }

def peaknames = ptest.peakfile

def peakinfoFilter = PeakInfo.createCriteria()
def pifilter = peakinfoFilter.list {

   'in'("fname", peaknames)


    projections
        {

           groupProperty "transF"

            count "fname"

        }


}

我想知道是否还有其他方法可以对一个查询执行此操作,而不是运行两个查询?

1 个答案:

答案 0 :(得分:0)

您可能可以执行以下操作。尚未执行,但您知道了。看看GORM的子查询部分。

def peakinfoFilter = PeakInfo.createCriteria()
def pifilter = peakinfoFilter.list {
   'in' "fname", PeResult.where{
                  conid == conid.toString()
                  fdr > "0.05"
            }. peakfile
    
    projections
    {
       groupProperty "transF"
       count "fname"
    }
}