I built working MySQL query:
SELECT
v.*, u.username
FROM
video AS v, users AS u
WHERE
v.type = 'public'
AND
v.user_ID = u.user_ID
Now I want to add a third table and count() results from the table comments where video_ID from this table will be equal to those from the table video.
I tried this but it wasn’t successful:
SELECT
v.*, u.username, count(c.video_ID)
FROM
video AS v, users AS u, comments AS c
WHERE
v.type = 'public'
AND
v.user_ID = u.user_ID
AND
v.video_ID = c.video_ID
In return I want to get the number of comments related to certain video_ID‘s.
I don’t understand how to make it work correctly in one query.
Could you please help me out?
Thank you in advance,
Ilia
If you are using an aggregate function like a
COUNTin a query, you need to specify the grouping with aGROUP BYclause.Try this
While MySQL lets you leave out the some elements of the
GROUP BYclause, it is good practice to specify them.When joining two tables, it is good practice to use the
INNER JOINsyntax, rather than aWHEREfilter.