Jump to content

MYSQL JOINS


aclab

Recommended Posts

Not entirely sure if this is possible... I've got two queries and I'm not sure if it is possible to merge the two as there is already a JOIN on the first the query...

 

Here is my current code (which works fine):

	$sql = "SELECT p.*, u.username
	FROM posts p
	LEFT OUTER JOIN users u ON (u.id = p.userID)
	WHERE parentID IS NULL
	LIMIT $offset, $resultsPerPage";
$result = mysql_query($sql);
while($record = mysql_fetch_assoc($result))
{
    # Count the replies for the parent message
    $record = $this->cleanArray($record,"load");
    $sql = "SELECT id FROM posts WHERE parentID = " . $record[id];
    $res = mysql_query($sql);
    $replies = mysql_num_rows($res);
        ...
        }

 

...so I'm looking to extend the first statement to also count the number of posts (where the parentID = p.id) for each record where the parentID = NULL

('posts' table contains child and parent records).

Hope that makes sense? Any help appreciated...

Thanks

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.