Prix
Prix

Reputation: 19528

Using left join for the same table or is there a better way?

I have a table called bans where I have the follow fields:

room_id, banned_user_id, banned_by_id, reason, ts_start, ts_end

The users data come from the table called users, now I wanted to query the bans to retrive the name of who was banned and by who along with reason, time the ban was placed and time it ends.

So I have this query:

SELECT u.username, us.username, b.reason, b.ts_start, b.ts_end
FROM `bans` b 
LEFT JOIN users us ON b.banned_by_uid = us.uid 
LEFT JOIN users u ON b.banned_uid = u.uid
WHERE room_id = 3

My question here is wether my query is ok by using the LEFT JOIN for the 2 data I have to grab from the table users or there is a different approach for this kinda of scenario ?

Upvotes: 1

Views: 83

Answers (1)

Ryan
Ryan

Reputation: 28187

Your query is perfectly acceptable. Each join to users is on a specific ID, which translates into a simple lookup, with minimal overhead.

Upvotes: 4

Related Questions