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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.