Re: Joining same table twice

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 ..

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux