因此,我一直试图找到一种在objection.js中执行以下sql操作的方法
SELECT
u.user_id, u.profile_photo_name, u.first_name,
u.last_name, COUNT(*) AS count
FROM users AS u
INNER JOIN post_users AS pu ON (u.user_id = pu.user_id)
WHERE
u.organization_id = 686
GROUP BY user_id
ORDER BY count DESC
LIMIT 1这就是我到目前为止.但不能以这种方式使用$relatedQuery
return await User.$relatedQuery('recognitions')
.where('organization_id', organizationID)
.select(
'user_id',
'profile_photo_name',
'first_name',
'last_name',
raw('COUNT(*) as count')
)
.groupBy('user_id')
.orderBy('count', 'desc')
.limit(1)这就是承认的关系:
recognitions: {
relation: Model.ManyToManyRelation,
modelClass: Post,
join: {
from: 'users.user_id',
through: {
from: 'post_users.user_id',
to: 'post_users.post_id',
},
to: 'posts.post_id'
}
},发布于 2019-03-15 19:03:51
必须使用joinRelation代替first()而不是limit(1)来获取单个对象,而不是长度为1的数组
return await User.query()
.joinRelation('recognitions')
.where('organization_id', organizationID)
.select(
'user_id',
'profile_photo_name',
'first_name',
'last_name',
raw('COUNT(*) as count')
)
.groupBy('user_id')
.orderBy('count', 'desc')
.first()https://stackoverflow.com/questions/55188050
复制相似问题