我正在尝试在Slick中重现此查询。
SELECT *
FROM A
JOIN LEFT B AS B1 ON B1.aId = A.id && B1.condition = 'b1'
JOIN LEFT B AS B2 ON B2.aId = A.id && B2.condition = 'b2'
- (no condition, the query in a plain way)
- WHERE B1.status = 'delete' OR B2.status = 'delete'
- WHERE ((B1.status = 'read' AND B2.status <> 'delete') OR (B1.status <> 'delete' AND B2.status = 'read')
- WHERE B1.status = 'write' AND B2.status = 'write'
- WHERE B1.status = 'full' AND B2.status = 'full'
- WHERE ((B1.status = 'full' AND B2.status = 'write') OR (B1.status = 'write' AND B2.status = 'full')
我不确定这是否可能
到目前为止,我有类似的东西
val query = for { ((a, b1Opt), b2Opt) <- ATable.aQuery join
BTable.BQuery on ((joinTable, bTable) => join._1.id === _.AId && bTable.condition === "b1") join
BTable.BQuery on ((joinTable, bTable) => join._1.id === _.AId && bTable.condition === "b2")
} yield (a, b1Opt, b2Opt)
我正在尝试类似的事情
val filterB = query {
case (a, b1Opt, b2Opt) => {
bStatus match {
case "delete" => b1Opt.map(b1 => b1.status === "delete") || b1Opt.map(b2 => b2.status === "delete")
}
}
}
答案 0 :(得分:1)
根据您的描述,表A的left join
上表B的两个连续id
应该转换为类似于以下内容的东西:
val joinQuery = for {
((a, b1), b2) <- tableA joinLeft tableB on ( (x, y) =>
x.id === y.aId && y.condition === "b1" )
joinLeft tableB on ( (x, y) =>
x._1.id = y.aId && y.condition === "b2" )
} yield (a, b1, b2)
并且where
的{{1}}条件应如下所示:
B1.status = 'delete' and B2.status = 'delete'
请注意,在val filterB = joinQuery.filter{ case (_, b1, b2) =>
b1.filter(_.status === "delete").isDefined && b2.filter(_.status === "delete").isDefined
}
中,left join
和b1
被包裹在b2
中,因此Option
使用isDefined
操作。
作为另一个说明,可能值得考虑在执行and
之前用B
将表B.condition = 'b?'
过滤为精简的B1
和B2