Re: SQL Join query

From: Date: Sat, 07 Aug 2004 16:23:10 +0000
Subject: Re: SQL Join query
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-193087@lists.php.net to get a copy of this message
Mattias Jönsson wrote:
SELECT pics.*, pic_comments.*, count(*) AS num_comments FROM pics, pic_comments WHERE pics.category = 1 AND pics.id = pic_comments.pic_id GROUP BY pic_comments.pic_id; [snip]
The problem is that i only get the the pictures that have comments, i want even the pictures where there arent no comments and the field num_comments should be 0.
Need a LEFT JOIN SELECT pics.*, pic_comments.*, count(*) AS num_comments FROM pics LEFT JOIN pic_comments on pics.id = pic_comments.pic_id WHERE pics.category = 1 GROUP BY pic_comments.pic_id -- John Holmes php|architect - The magazine for PHP professionals - http://www.phparch.com

« previous php.general (#193087) next »