php - Mysql复杂排序

标签 php mysql sorting

我有一个这样的查询

SELECT videos.*, 
       (SELECT COUNT(comment_id) 
        FROM comments 
        WHERE comments.comments_video_id = videos.id) AS video_comments
FROM videos
ORDER BY video_comments DESC

现在,如果我有 2 行具有相同的“videos_comments”计数,如何编写此代码,以便在这两个视频之间,首先显示评论表中具有最新评论 (comments_date) 的视频

最佳答案

SELECT videos.id, count(*) video_comments 
FROM videos LEFT JOIN comments 
ON comments.comments_video_id = videos.id
GROUP BY videos.id
ORDER BY video_comments DESC

关于php - Mysql复杂排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34915691/

相关文章:

php - 使用 REPLACE 和 LIKE 更新 MySQL

javascript - 当我从其他域的 coideigniter 中使用 ajax 进行调用时丢失 session

mysql - 如何调用常用的 MySQL 查询

mysql - 从一组条目中选择一个不同的行

c# - 对目录文件进行排序并获取最高的文件名

database - 在 relations() 方法中定义自定义排序标准

php - 在 php 中使用 simplexlsx 读取 excel xlsx 文件

php - laravel eloquent 中深度嵌套关系中的 withSum

mySQL开关: How to give "OR" priority or importance in a SELECT statement to get a default row

c - strcmp 未正确比较指向字符的指针数组中的 2 个相邻字符串