我有一个用户表:users
我有另一张桌子booths
users
===============================
ID | NAME | SID
-------------------------------
1 cillia abcd
2 Ted efgh
booth
=====================================================
BOOTH_ID | BOOTH_OWNER | BOOTH_MANAGER
-----------------------------------------------------
1 1 2
2 2 4
3 1 3
我想创建一个查询,明确特定展位,经理姓名和所有者姓名。
这样做的最佳方法是什么?
答案 0 :(得分:3)
您只需将users
表链接到booth
表两次:
SELECT b.BOOTH_ID, s1.NAME AS BoothOwner, s2.NAME AS BoothManager
FROM booth b
INNER JOIN users s1 ON b.BOOTH_OWNER = s1.ID
INNER JOIN users s2 ON b.BOOTH_MANAGER = s2.ID