I have two tables: comments & photos
Every comments is "connected" to a certain photo by the photo id. I want to get all photos, from a certain author(author_id), and a count of all the comments on the specific photo. I'm sorry for my english, but i think that's the best way i can describe it.
I want a table with:
- id (photo table)
- rating (photo table)
- created_at (photo table)
- number_of_comments (comments table)
What i've tried so far (with syntax error):
"SELECT p.id, p.rating, p.created_at, x.*
FROM photos p
LEFT JOIN
(
SELECT photo_id, COUNT(*) as cc
FROM comments
GROUP BY photo_id
) x
ON x.photo_id= p.id"
error: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"SELECT p.id, p.rating, p.created_at, x.* FROM photos p LEFT JOIN ( SELECT' at line 1"
photos table
- id
- author_id
- filename
- caption
- rating
- flags
- is_active
comments table
- id
- comment
- author_id
- photo_id
- created_at
- flags
- is_active