LEFT JOIN count likes

Asked

Viewed 49 times

1

I have the following tables in Mysql (it is more complex than the example, I simplified):

CURSO:   id | nome
CURTIDA: id | id_curso

Every time someone enjoys a course, feeds the table LIKES

Now, when I go to SELECT in the course, I would need to get the amount of likes, I did so:

SELECT *, count(curtida.id) AS curtidas FROM curso 
LEFT JOIN curtida ON curtida.id_curso = curso.id

But I’m not getting the right number of likes... and if the course has not enjoyed, nor appearing is on this select.

1 answer

2


You need to put a GROUP BY to group the data:

SELECT *, count(curtida.id) AS curtidas FROM curso 
LEFT JOIN curtida ON curtida.id_curso = curso.id
GROUP BY curso.id

Browser other questions tagged

You are not signed in. Login or sign up in order to post.