use*_*144 2 php kohana kohana-orm
我有2张桌子
用户和评论
评论与用户有多对一的关系
试着想出一种使用ORM根据评论量获得最高用户的方法
有什么建议?
您的查询应如下所示:
SELECT users.username, COUNT(comments.id) AS total
FROM users
INNER JOIN comments
ON users.id = comments.user_id
GROUP BY users.username
ORDER BY COUNT(comments.id) DESC
Run Code Online (Sandbox Code Playgroud)
转换为ORM:
ORM::factory('user')
->select('user.username', array('COUNT("comments.id")', 'total'))
->join('comments', 'INNER')
->on('user.id', '=', 'comments.user_id')
->group_by('user.username')
->order_by('total', 'DESC')
->find_all();
Run Code Online (Sandbox Code Playgroud)