我有 2 张桌子
Table1 - columns:
User1Id, User2Id, User3ID
Table2 - columns:
UserID, Name, Family
如何从 User1Id、User2Id、User3ID 中获取姓名和家庭?
select t1.user1id, t1.user2id, t1.user3id, t1.date, t1.comment
t2.name as name1, t2.family as family1,
t3.name as name2, t3.family as family2,
t4.name as name3, t4.family as family3
from table1 t1
left join table2 t2 on t1.user1id = t2.userid
left join table2 t3 on t1.user2id = t3.userid
left join table2 t4 on t1.user3id = t4.userid
SELECT Name, Family FROM Table1, Table2 WHERE Table2.UserID = Table1.User1Id
UNION
SELECT Name, Family FROM Table1, Table2 WHERE Table2.UserID = Table1.User2Id
UNION
SELECT Name, Family FROM Table1, Table2 WHERE Table2.UserID = Table1.User3ID