scarhand Posted February 12, 2010 Share Posted February 12, 2010 Having trouble...heres my query: $sql = mysql_query("select p.title, p.date_posted, p.date_deadline, p.payment_deposit, p.payment_total, p.payment_status, p.priority, (select count(r.*) as replycount from projects as p, replies as r where r.pid = p.id) from projects as p, replies as r") or die(mysql_error()); I'm just trying to grab the 7 fields from the projects table, and count how many replies there are for the project from the replies table. r.pid = project id (p.id) Quote Link to comment https://forums.phpfreaks.com/topic/191850-query-with-joined-count/ Share on other sites More sharing options...
scarhand Posted February 12, 2010 Author Share Posted February 12, 2010 this doesnt work properly either: $sql = mysql_query("select p.*, count(r.pid) as replycount from projects as p, replies as r where p.id = r.pid"); Quote Link to comment https://forums.phpfreaks.com/topic/191850-query-with-joined-count/#findComment-1011285 Share on other sites More sharing options...
kickstart Posted February 12, 2010 Share Posted February 12, 2010 Hi You need a GROUP BY clause. For example:- SELECT p.title, p.date_posted, p.date_deadline, p.payment_deposit, p.payment_total, p.payment_status, p.priority, COUNT(r.pid) as replycount FROM projects p INNER JOIN replies r ON p.id = r.pid GROUP BY p.title, p.date_posted, p.date_deadline, p.payment_deposit, p.payment_total, p.payment_status, p.priority All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/191850-query-with-joined-count/#findComment-1011299 Share on other sites More sharing options...
scarhand Posted February 16, 2010 Author Share Posted February 16, 2010 Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource This is the error I get using that query, kickstart Quote Link to comment https://forums.phpfreaks.com/topic/191850-query-with-joined-count/#findComment-1012994 Share on other sites More sharing options...
kickstart Posted February 16, 2010 Share Posted February 16, 2010 Hi Put the "or die" code back onto the mysql_query line so we can see what the real error is. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/191850-query-with-joined-count/#findComment-1013051 Share on other sites More sharing options...
scarhand Posted February 17, 2010 Author Share Posted February 17, 2010 got it working, you were right about the grouping select p.*, count(r.pid) as replycount from projects as p, replies as r where r.pid = p.id group by r.pid Quote Link to comment https://forums.phpfreaks.com/topic/191850-query-with-joined-count/#findComment-1013503 Share on other sites More sharing options...
scarhand Posted February 17, 2010 Author Share Posted February 17, 2010 even better: SELECT p.*, count(r.pid) replycount FROM projects p LEFT JOIN replies r ON r.pid = p.id GROUP BY r.pid Quote Link to comment https://forums.phpfreaks.com/topic/191850-query-with-joined-count/#findComment-1013565 Share on other sites More sharing options...
kickstart Posted February 17, 2010 Share Posted February 17, 2010 Hi While MySQL doesn't mind having only only a few columns in the GROUP BY clause, plenty of other flavours will throw an error if you don't have all the non aggregate columns listed in the GROUP BY clause. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/191850-query-with-joined-count/#findComment-1013602 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.