Jump to content

query with joined count....


scarhand

Recommended Posts

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)

Link to comment
https://forums.phpfreaks.com/topic/191850-query-with-joined-count/
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.