Reputation: 433
I need to fix following query:
SELECT *
FROM message_treads
LEFT JOIN usernames ON usernames.uid=message_treads.uid_1
LEFT JOIN usernames ON usernames.uid=message_treads.uid_2
I know i should use aliases, but i am not sure on how to do it here. I know how to do it when there is only one left join of one table, but how to to it when there are more than one?
Upvotes: 0
Views: 4451
Reputation: 17020
You are correct, you need to use aliases like so:
SELECT *
FROM message_treads
LEFT JOIN usernames uid1 ON uid1.uid=message_treads.uid_1
LEFT JOIN usernames uid2 ON uid2.uid=message_treads.uid_2
Upvotes: 2
Reputation: 360572
You can do this:
SELECT *
FROM message_treads
LEFT JOIN usersnames ON usernames.uid IN (message_treads.uid_1, message_tread.uid_2)
As for aliasing, you'd do it like this:
SELECT *
FROM message_treads
LEFT JOIN usernames AS user1 ON user1.uid = message_treads.uid_1
LEFT JOIN usernames AS user2 ON user2.uid = message_treads.uid_2
But that's probably not what you want, as this'll return two sets of columns from the doubled-up usernames tables. The first version above will return only one set of username columns.
Upvotes: 3