Re: help with query

From: Date: Thu, 07 Jun 2012 18:44:46 +0000
Subject: Re: help with query
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-318186@lists.php.net to get a copy of this message
Jack <JackListMail@Gmail.com> wrote: >Hello All, > > > >I have this string defined for my query and it shows the different >types of >categories fine, but when I change a.categoryid = c.categoryid to >a.categoryid = 1 which is only one of the categories > >It shows me the same record twice. > > > >$query = "select a.startdate, a.articleid, c.name, a.title, a.intro, >a.datecreated from articles as a, categories as c where (a.startdate = >-1 or >a.startdate <= {$now}) and (a.enddate = -1 or a.enddate >= {$now}) and >a.categoryid = c.categoryid order by a.startdate DESC"; > > > > > > while ( $row = mysql_fetch_array($res) ) { > > $tpldata['articles'][] = array( > > 'title' => $row['title'], > > 'intro' => makeLinks($row['intro']), > > 'id' => $row['articleid'], > > 'categoryname' => $row['name'], > > 'created' => date('n/j/Y', >$row['datecreated']) > > ); > > //echo $row['datecreated']; > > } > > > >Any Ideas??? > > > > > >Thanks! > > How many entries in total are in your categories table? You're joining the table but not putting any kind of constraint on the joined tables, so its doing a join on all the rows. You can either add an extra clause on the join, leave the original join and limit with a where clause, or use a group by on that field. Thanks, Ash http://ashleysheridan.co.uk

« previous php.general (#318186) next »