Skip to content
Advertisement

How to ORDER BY a column from INNER JOIN

Table structure from comments:

Table structure from cmt_likes:

As you can see on the table cmt_likes, on the home page the comment from the user 324 have 3 likes, and the comment from the user 71 have 2 likes (i use the number of rows, that have the same cmt_id and slug to count the likes).

This is my current sql, this is only for display the comments:

But i want to ORDER BY the number of likes.

so i tried:

But this sql only return the rows that have ‘likes’. If you look at my tables you will see that the comment from the user 565 don’t have likes, so this row is not returning in the above sql.

This is my current result with the above sql:

And this is what i expected:

Advertisement

Answer

You need left joins. I also recommend table aliases:

However, I think you also need aggregation before the JOIN:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement