Jump to content

Selecting the same value twice using different AS clauses


Jibberish

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

Archived

This topic is now archived and is closed to further replies.

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