最有效的方法是将结果与 JOIN 语句组合在一起;
SELECT T1.*, F1.Friend
FROM Table_1 as T1
LEFT JOIN Table_2 as F1
ON T1.id = F1.id
然后遍历 mysql 客户端中的每一行。
另一种方法是对同一个表进行多次连接,但如果没有更好的方法来区分各个朋友记录,您最终会在 JOIN 语句中进行一些混乱的排除。
SELECT T1.*, F1.Friend, F2.Friend, F3.Friend, [snip/] F8.Friend
FROM Table_1 as T1
LEFT JOIN Table_2 as F1
ON T1.id = F1.id AND F1.Friend NOT IN (F2.Friend, F3.Friend, [snip/] F8.Friend)
LEFT JOIN Table_2 as F2
ON T1.id = F2.id AND F2.Friend NOT IN (F1.Friend, F3.Friend, [snip/] F8.Friend)
LEFT JOIN Table_2 as F3
ON T1.id = F3.id AND F3.Friend NOT IN (F1.Friend, F2.Friend, F4.Friend, [snip/] F8.Friend)
[big snip/]
LEFT JOIN Table_2 as F8
ON T1.id = F8.id AND F8.Friend NOT IN (F1.Friend, F2.Friend, [snip/] F7.Friend)