scala 多个连接光滑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18537925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
multiple joins with slick
提问by dsr301
For joining between two tables is done like
两个表之间的连接是这样完成的
(for {
(computer, company) <- Computers leftJoin Companies on (_.companyId === _.id)
if computer.name.toLowerCase like filter.toLowerCase()
}
But in case if joining required between more tables what is the right way trying below but doesnt work
但是,如果在更多表之间需要连接,下面尝试的正确方法是什么但不起作用
(for {
(computer, company,suppliers) <- Computers leftJoin Companies on (_.companyId === _.id)
//not right leftjoin Suppliers on (_.suppId === _.id)
if computer.name.toLowerCase like filter.toLowerCase()
}
回答by cvogt
The first join results in a Query returning Tuples. One of the tuple components has the foreign key you want to use for the second join. You need to get this component in the second join condition before getting its field. If Companies is the table, that has the field suppId it would look like this:
第一个连接导致 Query 返回元组。元组组件之一具有要用于第二个连接的外键。在获取其字段之前,您需要在第二个连接条件中获取此组件。如果 Companies 是表,则该表具有 suppId 字段,它将如下所示:
(for {
((computer, company),suppliers) <- Computers leftJoin Companies on (_.companyId === _.id) leftJoin Suppliers on (_._2.suppId === _.id)
if computer.name.toLowerCase like filter.toLowerCase()
} yield ... )
回答by pedrofurla
I think you want this:
我想你想要这个:
for {
(computer, company) <- Computers leftJoin Companies on (_.companyId === _.id)
(supp, _) <- company innerJoin Suppliers on (_.suppId === _.id)
if computer.name.toLowerCase like filter.toLowerCase()
} yield (computer, company, supp)
Of course, I am making assumptions about your model. Not sure if suppliers are linked with companies or computers.
当然,我正在对您的模型进行假设。不确定供应商是否与公司或计算机相关联。

