【发布时间】:2017-11-06 22:28:59
【问题描述】:
如何在 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
【问题讨论】:
-
您尝试了哪些方法,为什么没有成功?你只是在找this function吗?
-
我没有立刻意识到这一点,但是除了
sub_select,还有subList_select。 -
@user2407038 返回一个
SqlExpr,并且仅限于单行值。这行不通。 -
@JezenThomas
subList_select也返回一个 SqlExpr。也不行。