MySQL – Join Results and Sum of Likes in Different Table

MySQL

I have two databases, articles and article-likes.
I need the query to select everything from the article and add a row which is the sum of likes (we have -1 for dislike and +1 for like) from the table article-likes by searching for their ID.

Articles table:
Articles
Article-Likes table:
Article-likes

For this instance, the article with ID Lorem-Ipsum should have 2 likes. On the other hand, Sit-Amet should have 1 like (1 + -1).

Best Answer

You can use a subquery to sum all likes of each article.

SELECT author,
       name,
       text,
       id,
       (SELECT SUM(value)
        FROM Articles-Likes
        WHERE articles_id = Articles.ID) as Likes  
from   Articles