【发布时间】:2023-03-07 00:51:01
【问题描述】:
我有 2 个表格,在第一个 cmets 和文章 id 中,在第二个文章标题中,id ,文章类别。我想要一个拥有最多 cmets 的文章的标题。
SELECT comments.article_id, news.title, news.category_id,
COUNT(comments.id) as counts
FROM comments
JOIN news ON news.id = comments.article_id
GROUP BY(article_id)
ORDER BY counts DESC
LIMIT 3
我试过这个:
$articles = DB::table('comments')
->join('news', 'news.id', '=', ' comments.article_id')
->select(comments.article_id', 'news.title', ' news.category_id')
->count('comments.id')
->groupBy('article_id')
->orderBy(DB::raw('count(comments.id)', 'desc')
->limit(3)
->get();
但是有:
Call to a member function groupBy() on integer
【问题讨论】: