Answer the question
In order to leave comments, you need to log in
Display news based on number of comments and current date in Yii2?
Hi all.
Faced such a task, there is a table with News and Comments, how to sort the output of News Desc depending on the date of publication, there are no problems
'query' => News::find()->where(['visibility'=>1])->orderBy('date DESC');
Answer the question
In order to leave comments, you need to log in
Not?
SELECT *
FROM news n
INNER JOIN news_comments nc ON n.news_id = nc.news_id
GROUP BY n.news_id
ORDER BY COUNT(nc.comment_id) DESC, n.date DESC
News::find()->where(['news.visibility'=>1])
->innerJoin(['nc' => NewsComments::tableName()], 'nc.news_id = news.id')
->groupBy('news.id')
->orderBy('COUNT(nc.id) DESC, news.date DESC');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question