SQL从另一个表中排序

时间:2017-04-03 11:29:11

标签: php sql sql-order-by

我正试图通过upvote在我的页面上显示我的帖子。

我的表格帖子

(id, post_name, post_description, post_category, post_subcategory)

和表投票

(id, post_id, vote_rank)

我可以用

显示我的所有帖子
$bdd->query('SELECT * FROM posts WHERE post_subcategory = "'.$_GET['s'].'" ORDER BY post_name');

我尝试了JOIN但没有出现。

$bdd->query('SELECT *
            FROM posts AS p
               JOIN votes as v ON v.post_id = p.id
            WHERE p.post_subcategory =  "'.$_GET['s'].'" 
            ORDER BY v.vote_rank');

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

$bdd->query('SELECT p.*
        FROM posts p LEFT JOIN votes v ON v.post_id = p.id
        WHERE p.post_subcategory =  "'.$_GET['c'].'" 
        ORDER BY v.vote_rank');

试一试

答案 1 :(得分:0)

尝试使用内连接

$bdd->query('SELECT p.*
        FROM posts p inner join votes v on v.post_id = p.id
        where p.post_subcategory =  "'.$_GET['c'].'" 
        order by v.vote_rank');