Re: Joining same table twice

From: Date: Sun, 12 Dec 2004 02:05:13 +0000
Subject: Re: Joining same table twice
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-204083@lists.php.net to get a copy of this message
Sebastian wrote:
i apologize if its against the rules to post mysql questions here, though im using php to query the db so... i have a table with three integers. [table post] postuserid | replyuserid | parentid ---------------------------------- i do a left join on table user to get the username that postuserid belongs to, but i also need to get the username on the same table 'user' of replyuserid as well if parentid isn't null. can this be done with a single query? this is what i have: mysql_query(" SELECT post.postuserid, post.replyuserid, post.parentid, user.username AS uname FROM post LEFT JOIN user ON(user.userid = post.postuserid) ORDER BY id"); that gets me the username of postuserid, but what about replyuserid, how i get that username? thanks for any help.
If you are using the same table more than once you can alias it and use the alias in your select, where, order by etc. clauses. If more columns have the same name, you can alias them too to get distinct array keys from mysql_fetch_assoc() Useless example: select t1.id t1_id, t2.id t2_id from table t1 left join table t2 on t1.id = t2.id ..

« previous php.general (#204083) next »