I'm no SQL pro, but I have the following statement that works... but I want to add something else to it.
I have another table, comments, that may or may not have a list of comments related to the p.id of the photos table. something like c.photo=p.id. I want to get the average of the ratings in those comments if they exist. How can I manipulate the above query to get what I want?
I've tried something like this to no avail:
And before you ask, yes pp_photos has a field 'id'.. I just dont think I'm formulating it correctly.
Code:
SELECT p.id,p.user,p.userid,p.cat,p.storecat,p.ismature,p.title,p.bigimage,p.height,p.width,u.location FROM pp_photos p, pp_users u WHERE p.approved=1 AND p.storecat=0 AND p.userid=u.userid ORDER BY date DESC LIMIT 5;
I have another table, comments, that may or may not have a list of comments related to the p.id of the photos table. something like c.photo=p.id. I want to get the average of the ratings in those comments if they exist. How can I manipulate the above query to get what I want?
I've tried something like this to no avail:
Code:
?SELECT p.id,p.user,p.userid,p.cat,p.storecat,p.ismature,p.title,p.bigimage,p.height,p.width,u.location,avg(c.rating) AS prating FROM pp_photos p, pp_users u LEFT JOIN pp_comments c ON (c.photo=p.id) WHERE p.approved=1 AND p.storecat=0 AND p.userid=u.userid ORDER BY date DESC LIMIT 5;
ERROR 1054 (42S22): Unknown column 'p.id' in 'on clause'