通过等效的光滑查询

时间:2016-09-26 11:30:43

标签: postgresql scala slick

以下是Postgresql查询,它返回400多行

SELECT 
*

FROM 
investors, 
investments, 
investor_personal_information,
sub_products, 
products, 
company, 
issuers, 
investment_admin_approvals, 
users

WHERE 
investments.investor_id = investor_personal_information.id AND
investments.investor_id = investors.id AND 
investments.sub_product_id = sub_products.id AND 
sub_products.product_id = products.id AND 
products.company_id = company.id AND 
investments.id = investment_admin_approvals.investment_id AND 
issuers.owner = users.id

我在Scala中编写了等效的Slick查询

val query = {
  val baseQuery = Investments join InvestorPersonalInformation on {
    case (its, ipi) => its.investorId === ipi.id
  } join Investors on {
    case ((its, ipi), irs) => its.investorId === irs.id
  } join SubProducts on {
    case (((its, ipi), irs), sbp) => its.subProductId === sbp.id
  } join Products on {
    case ((((its, ipi), irs), sbp), pds) => sbp.productId === pds.id
  } join Tables.Company on {
    case (((((its, ipi), irs), sbp), pds), cpy) => pds.companyId === cpy.id
  } join Tables.Issuers on {
    case ((((((its, ipi), irs), sbp), pds), cpy), iss) => cpy.issuerId === iss.id
  } join InvestmentAdminApprovals on {
    case (((((((its, ipi), irs), sbp), pds), cpy), iss), iaa) => its.id === iaa.investmentId
  } join Users on {
    case ((((((((its, ipi), irs), sbp), pds), cpy), iss), iaa), usrs) => iss.owner === usrs.id
  } map {
    case ((((((((its, ipi), irs), sbp), pds), cpy), iss), iaa), usrs) => (its, ipi, irs, sbp, pds, cpy, iss, iaa, usrs)
  }

  baseQuery
}

当我运行db.run(query.result)时,我只获得12行。我在两个查询中都使用内连接,两者都是等价的。这可能是什么原因?

2 个答案:

答案 0 :(得分:2)

SQL查询不包含company.issuer_id = issuers.id上的联接。

答案 1 :(得分:0)

如果你是3.0及以上的光滑。打印由slick生成的sql语句,并与原始sql查询进行比较。

以下代码将帮助您获得由光滑生成的SQL

query.result.statements.foreach(println)