Jibberish Posted December 10, 2009 Share Posted December 10, 2009 For a simple example I have a user table with the fields user_id and user_name in it, an other table that joins two users up with the fields id, FK_user_id_start, FK_user_id_finish, both the FK reference a user_id. Is there a way based on an id in the seconds table to return a result using AS (i.e. SELECT users.username AS user_start_name FROM....) that look like this without doing two queries. id | users_start_name | users_finished_name | 0 Bob Ben 1 Tom Peter I just cant think of a way to do it, and cant think of a good way to phrase it so that google can help me Quote Link to comment https://forums.phpfreaks.com/topic/184629-selecting-the-same-value-twice-using-different-as-clauses/ Share on other sites More sharing options...
JustLikeIcarus Posted December 10, 2009 Share Posted December 10, 2009 Yeah what you want to do is pull in the users table twice using aliases. I dont know your tables/columns but an example would be select id, t1.username as start_name, t2.username as end_name from fields, users t1, users t2 where t1.user_id = fields.user_id_start and t2.user_id = fields.user_id_end Quote Link to comment https://forums.phpfreaks.com/topic/184629-selecting-the-same-value-twice-using-different-as-clauses/#findComment-974768 Share on other sites More sharing options...
Jibberish Posted December 10, 2009 Author Share Posted December 10, 2009 Thanks a lot, helped me out no end. Spent ages trying different things in google to find out how to do it Quote Link to comment https://forums.phpfreaks.com/topic/184629-selecting-the-same-value-twice-using-different-as-clauses/#findComment-974791 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.