如何在子查询中实现计数

时间:2013-03-01 18:23:20

标签: grails gorm

我有以下表结构:

TABLE A:
ID
COL1
COL2
...
COL(n)

TABLE B:
ID
A_ID (id in table A)
VALUE

A-> B的一对多关系

class A {
    int id
    ...
    coln
    Set<String> bSet

    static hasMany = [bSet: B]

    static mapping = {
        restrictions joinTable: [name: "B", key: "A_ID", column: "VALUE"]
    }
}

如何构建条件以便执行如下查询:

select table1.* from A table1 where (select count(*) from B table2 where table2.A_ID = table1.ID and table2.VALUE in ('excluded_value_1','excluded_value_2')) = 0

1 个答案:

答案 0 :(得分:0)

尝试一下,显然未经测试,但可能会给你一些东西从

开始
def a = A.createCriteria()
a.list {
 createAlias("bSet", "b", CriteriaSpecification.LEFT_JOIN)
 not {
    'in'("b",['excluded_value_1','excluded_value_2'])
 }
}