简体   繁体   中英

SQL - How can I change this code of left outer join to right outer join?

I have this following SQL code which needs to be converted to right outer join. As I'm still on the learning path of SQL, can I simply change LEFT JOIN to RIGHT JOIN?

SELECT message, count(comment_likes.id) AS total_likes
FROM comments
LEFT JOIN comment_likes ON comment_id = comments.id
GROUP BY message
ORDER BY total_likes DESC

You just reverse the tables:

SELECT message, count(comment_likes.id) AS total_likes
FROM comment_likes RIGHT JOIN
     comments
     ON comment_id = comments.id
GROUP BY message
ORDER BY total_likes DESC;

That said, I (and others) have a strong preference for LEFT JOIN because it says "keep everything in the table you just saw" rather than "keep everything in some table that you will eventually see as you read the FROM clause".

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