[英]Select from subquery and join on subquery in Esqueleto
如何在Esqueleto中select ... from (select ...) join (select ...)
?
我知道我可以使用Persistent中的rawSql
,但我想避免這種情況。
對於記錄,這是完整的查詢:
select q.uuid, q.upvotes, q.downvotes, count(a.parent_uuid), max(a.isAccepted) as hasAccepted
from
(select post.uuid, post.title, sum(case when (vote.type = 2) then 1 else 0 end) as upvotes, sum(case when (vote.type = 3) then 1 else 0 end) as downvotes
from post left outer join vote on post.uuid = vote.post_id
where post.parent_uuid is null
group by post.uuid
order by post.created_on desc
) q
left outer join
(select post.parent_uuid, max(case when (vote.type = 1) then 1 else 0 end) as isAccepted
from post left outer join vote on post.uuid = vote.post_id
where post.parent_uuid is not null
group by post.id
) a
on a.parent_uuid = q.uuid
group by q.uuid
limit 10
我來到這里因為我有同樣的問題。 我想我們想要的東西是這樣的:
fromSelect
:: ( Database.Esqueleto.Internal.Language.From query expr backend a
, Database.Esqueleto.Internal.Language.From query expr backend b
)
=> (a -> query b)
-> (b -> query c)
-> query c
不幸的是,從查看Database.Esqueleto.Internal.Sql .FromClause:
-- | A part of a @FROM@ clause.
data FromClause =
FromStart Ident EntityDef
| FromJoin FromClause JoinKind FromClause (Maybe (SqlExpr (Value Bool)))
| OnClause (SqlExpr (Value Bool))
我不認為在Esqueleto有任何支持。 它似乎只支持簡單的表名和連接與具有布爾表達式的on子句。 我想最難添加支持的是處理表和列名別名( as
sql子句),因為^.
期望expr (Entity val)
和EntityField val typ
。 最簡單的方法是將其更改為對兩個操作數使用String
或Text
,但這不是非常類型安全的。 我不確定什么樣的最佳選擇才能使該類型安全。
編輯:可能最好忘記^.
並且在提供其第一個參數的返回值作為其第二個參數的參數時,使用fromSelect
生成別名。 可能必須更改類型以為這些別名騰出空間。 這只是考慮from
子查詢,而不是連接。 那是另一個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.