select ... from (select ...) join (select ...)
在 Esqueleto我该怎么做?
我知道我可以rawSql
从 Persistent 使用,但我想避免这种情况。
作为记录,这里是完整的查询:
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