简体   繁体   中英

SQL Order by from another table

I'm trying to display my posts by upvote on my page.

My table posts :

(id, post_name, post_description, post_category, post_subcategory)

and table votes :

(id, post_id, vote_rank)

I can display all my post with

$bdd->query('SELECT * FROM posts WHERE post_subcategory = "'.$_GET['s'].'" ORDER BY post_name');

I tried with JOIN but nothing appears.

$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');

Any idea?

$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');

Try It

Try using the inner join

$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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM