光滑的聚合子查询
agregation subqueries in slick
我想像这样使用 slick-way 进行子查询:
select suppliers.*,
(select count(1) from cofees
where cofees.sup_id == suppliers.sup_id
)
from suppliers
并将一个映射到类型 Seq[(Supplier, Int)]
还有类似的请求
select suppliers.*
where cofees.sup_id == suppliers.id)
from suppliers where not exists (select 1 from cofees
where cofees.sup_id == suppliers.sup_id)
我只找到了普通的-sql方式:
db.run {sql"""select sup_id, name
where cofees.sup_id == suppliers.id)
from suppliers where not exists (select 1 from cofees
where cofees.sup_id == suppliers.sup_id)
"""}
但我想要
db.run{
for {
s <- suppliers
c <- counts(???)
} yield( s, c)
}
db.run{
for {
s <- suppliers if not exists (???)
} yield(s)
}
可能吗?
第一个应该像这样工作 - 使用左连接也可以获得零咖啡(如果这是你想要的)
for {
(s, c) <- suppliers joinLeft coffees on (_.sup_id===_.id)
} yield(s, c)
.groupBy(_._1)
.map { case (s, c) => (s, c.length) }
第二,猜你可以添加 if c.length === 0
根据 Adel 的建议进行编辑 - 它缺少 groupBy
我想像这样使用 slick-way 进行子查询:
select suppliers.*,
(select count(1) from cofees
where cofees.sup_id == suppliers.sup_id
)
from suppliers
并将一个映射到类型 Seq[(Supplier, Int)]
还有类似的请求
select suppliers.*
where cofees.sup_id == suppliers.id)
from suppliers where not exists (select 1 from cofees
where cofees.sup_id == suppliers.sup_id)
我只找到了普通的-sql方式:
db.run {sql"""select sup_id, name
where cofees.sup_id == suppliers.id)
from suppliers where not exists (select 1 from cofees
where cofees.sup_id == suppliers.sup_id)
"""}
但我想要
db.run{
for {
s <- suppliers
c <- counts(???)
} yield( s, c)
}
db.run{
for {
s <- suppliers if not exists (???)
} yield(s)
}
可能吗?
第一个应该像这样工作 - 使用左连接也可以获得零咖啡(如果这是你想要的)
for {
(s, c) <- suppliers joinLeft coffees on (_.sup_id===_.id)
} yield(s, c)
.groupBy(_._1)
.map { case (s, c) => (s, c.length) }
第二,猜你可以添加 if c.length === 0
根据 Adel 的建议进行编辑 - 它缺少 groupBy