Re: PHP/mySQL Query

From: Date: Fri, 06 Jul 2001 04:15:34 +0000
Subject: Re: PHP/mySQL Query
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-56440@lists.php.net to get a copy of this message
"Jeff Lewis" <jeff@hyrum.net> wrote: > I fought the urge to post this here but have to :( > > owners > -ownerID > -teamname > -more fields > > teampages > -ownerID > -bio > > Anyway, I'm doing a select on the database like this: select ownerID, > last_update FROM teampages ORDER BY last_update DESC LIMIT 10 > > The thing is, I want to get the team name from the other table as well. Can > anyone help me out? I've read the other solutions, they all used STRAIGHT JOINs. You may want to consider a LEFT JOIN (form of an OUTER JOIN). A LEFT JOIN will return all the records from table A and those from table B that match the records from table A. The implication is that with a straight join if there is no record for an ownerID corresponding to the record from the table teampages then the record from teampages won't be returned. So if a teamname never got entered in owners or the ID was mis-entered your query would not return all of the records from teampages and you probably want it to. Using a LEFT JOIN the record from teampages will be returned, but since there's no corresponding record in table owners the field teamname will be blank. Assuming all of the data is in both tables it's not a problem, but believe me at some point when doing database programming this issue will arise. SELECT teampages.ownerID, teampages.last_update, owners.teamname FROM teampages LEFT JOIN owners ON teampages.ownerID = owners.ownerID ORDER BY last_update DESC LIMIT 10 -- Steve Werby President, Befriend Internet Services LLC http://www.befriend.com/

« previous php.general (#56440) next »