我在 rethinkdb 中实现以下 SQL 查询时遇到了一些问题,我想根据 user_count 获得社区中最受欢迎的 5 个频道。
SELECT
channels.*,
COUNT(distinct channel_users.user_id) as user_count
FROM channel_users
LEFT JOIN channels ON
channels.id = channel_users.channel_id
WHERE channels.community_id = "MY_COMMUNITY_ID" AND channels.type = 'public'
GROUP BY channel_id
ORDER BY user_count DESC
LIMIT 5
这就是我在 ReQL 中得到的,它只是给了我一个通道列表,我怀疑这里需要更多的映射/减少?
r.db('my_db')
.table('channel_users')
.filter({ community_id : 'MY_community_id' })
.orderBy(r.desc('created_at'))
.eqJoin('channel_id', r.table('channels'))
.map(function(doc){
return doc.merge(function(){
return {
'left' : null,
'right': {'user_id': doc('left')('user_id')}
}
})
})
.zip()
.run(function(err, channels){
console.log(err, channels);
next();
});
桌子设计看起来像:
频道用户
id | channel_id | community_id | role | user_id
渠道
id | community_id | name | user_id (creator)
任何帮助表示赞赏!谢谢