开发者

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 diff开发者_运维百科erent approach for this kinda of scenario ?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜