MySQL(最热门)排序2个不同的参数(最喜欢的时间更短)

时间:2017-08-14 08:51:37

标签: mysql

我一直在努力弄清楚如何"Hottest Posts"选择帖子,most upvoted in shortest time选择帖子。

我有2个数据库表:

fun_posts:

Image1

fun_post_upvotes:

Image2

我的最新代码仍然不起作用:

$stmt = $this->conn->prepare("SELECT * FROM fun_posts, (SELECT image_id, COUNT(*) FROM fun_post_upvotes GROUP BY image_id)
            WHERE id = image_id ORDER BY id DESC, upvotes DESC");

1 个答案:

答案 0 :(得分:1)

尝试使用此查询:

$stmt = $this->conn->prepare=("SELECT * , (SELECT COUNT(*) 
                                           FROM fun_post_upvotes 
                                           WHERE image_id=fun_posts.id
                                           GROUP BY image_id)
                               FROM fun_posts
                               ORDER BY fun_posts,id DESC, fun_posts.upvotes DESC");