拜托,帮助我:
我有下表:
单元
用户
率
我不明白如何从SQL创建正确的条件结构:
代码:
SELECT * FROM Unit WHERE id not in (SELECT unit_id FROM Rate WHERE user_id = 55);
我看到了this回答。但我不明白如何将Condition链接到另一个表(实体)。
答案 0 :(得分:1)
查看您的标签我认为您需要SQL查询的Criteria视图;假设Unit
和Rate
类:
// This is the subquery
DetachedCriteria subquery = DetachedCriteria.forClass(Rate.class)
.add(Restrictions.eq("user_id", 55))
.setProjection(Projections.id())
// This corresponds to (SELECT * FROM Unit WHERE id not in (subquery))
Criteria criteria = session
.createCriteria(Unit.class)
.add(Subqueries.notIn("id", subquery));
答案 1 :(得分:0)
我不知道IN在这种情况下是最合适的。尝试使用内部联接来链接表:
SELECT * FROM Unit INNER JOIN Rate ON Rate.unit_id = Unit.id
WHERE Rate.user_id = 55