LEFT JOIN tanned

2

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

CURSO:   id | nome
CURTIDA: id | id_curso

Every time someone takes a course, they feed the CURTIDA table

Now, when I'm going to SELECT the course, I'd have to get the amount of tanned, I did this:

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 tanned ... and if the course has not tanned, nor appearing is in this select.

    
asked by anonymous 19.02.2018 / 18:16

1 answer

3

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
    
19.02.2018 / 18:22