我如何在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发布于 2018-10-10 00:17:06
我来这里是因为我也有同样的问题。我想我们想要的东西是这样的:
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子查询,而不是连接。这是另一个问题。
https://stackoverflow.com/questions/47146909
复制相似问题