考虑这个查询:
select user_id
from user_role a
where a.role_id = -1
and not exists (select 1
from user_role b
where b.role_id != a.role_id
and b.user_id = a.user_id);
我正在尝试使用 gorm 标准重新创建它。是否可以?如何添加 exists 子句?
更新
我一直在尝试这样解决:
UserRole.createCriteria().list{
eq('role', roleObj)
createAlias('user', 'u')
not{
inList('u.id', {
not {
eq('role', roleObj)
}
projections {
property 'user.id'
}
})
}
}
还是不行。我在执行它时遇到了这个错误:
DUMMY$_closure1_closure2_closure3_closure4 cannot be cast to java.lang.Long
我不明白错误信息。内部标准返回 ids 列表,如果我用 longs 列表替换内部标准,它就可以工作。有什么线索吗?
提前致谢
最佳答案
未测试:尝试 Sql 限制
UserRole.createCriteria().list {
and{
eq('roleId',-1)
sqlRestriction(" not exists(select 1
from UserRole b
where ............
)")
}
-- 希望您已将您的域命名为 UserRole 并将列名命名为 roleId。
关于grails - 条件中存在子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25308387/