channel 有许多 post, post 属于某个 channel. 现在想查 post 数最多的 channel , sql 如下。
请问 如果不用 find_by_sql 要怎么写这个查询呢?谢谢啦
SELECT
channels.*, count_t.p_count
FROM
channels ,
(
SELECT
channel_id ,
count(posts.id) AS p_count
FROM
posts
GROUP BY
posts.channel_id
ORDER BY
p_count DESC
LIMIT 10 OFFSET 0
) AS count_t
WHERE
channels.id = count_t.channel_id