Reputation: 97
I have 2 tables:
posts: userid, lastuserid
users: id, name
I need to join posts.userid = users.id
and posts.lastuserid = users.id
to get username
and lastusername
.
My query did as below:
SELECT posts. * , users.name, vUsers.name
FROM posts
INNER JOIN users ON users.id = posts.userid
INNER JOIN Users ON vUsers.id = posts.lastuserid
Is there any other (better) way to do this?
Upvotes: 0
Views: 55
Reputation: 1269443
Your query is probably correct. I would encourage you to use table aliases that are abbreviations for the things you are looking for:
SELECT p. * , u.name as username, l.name as lastusername
FROM posts p INNER JOIN
users u
ON u.id = p.userid INNER JOIN
users l
ON l.id = p.lastuserid;
Your query has something called vUsers
, which is not defined.
Upvotes: 2