gpt4 book ai didi

Scala Slick 2 加入多个领域?

转载 作者:行者123 更新时间:2023-12-04 15:22:34 25 4
gpt4 key购买 nike

如何在多个字段上进行连接,例如下面的示例?

val ownerId = 1
val contactType = 1
...
val contact = for {
(t, c) <- ContactTypes leftJoin Contacts on (_.id === _.typeId && _.ownerId === ownerId)
if t.id === contactType
} yield (c.?, t)

如何使用 Slick 2.0.1 实现这一目标? Idelly 我需要巧妙地生成这种查询
SELECT
x2."contact_id",
x2."type_id",
x2."owner_id",
x2."value",
x2."created_on",
x2."updated_on",
x3."id",
x3."type",
x3."model"
FROM
(
SELECT
x4."id" AS "id",
x4."type" AS "type",
x4."model" AS "model"
FROM
"contact_types" x4
)x3
LEFT OUTER JOIN(
SELECT
x5."created_on" AS "created_on",
x5."value" AS "value",
x5."contact_id" AS "contact_id",
x5."updated_on" AS "updated_on",
x5."type_id" AS "type_id",
x5."owner_id" AS "owner_id"
FROM
"contacts" x5
)x2 ON x3."id" = x2."type_id" AND x2.owner_id = 1
WHERE
(x3."id" = 3)

请注意 ON x3."id"= x2."type_id"AND x2.owner_id = 16

最佳答案

好的,所以在挖掘了网站和源代码之后,我想我终于找到了解决方案

leftJoin on() 方法接受以下参数 预测:(E1,E2)=> T ,所以我们可以这样做

val contacts = for {
(t, c) <- ContactTypes leftJoin Contacts on ( (type, contact) => {
type.id === contact.typeId && contact.ownerId === ownerId
} )
} yield (c.?, t)

根据需要生成 sql 查询。

关于Scala Slick 2 加入多个领域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23470534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com