Jump to content

Selecting the same value twice using different AS clauses


Recommended Posts

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 :P

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.