sql查询使用先前查询的结果

时间:2017-02-11 16:38:55

标签: mysql sql

如何查询表并从引用同一表的外键中提取相同的列信息? “SELECT FROM FROM FROM INNER JOIN gift ON to = id WHERE id = 1;”会让我得到第一部分,但我不确定如何获得第二部分 enter image description here

1 个答案:

答案 0 :(得分:2)

您可以加入users表两次:

select
    u1.name `from`,
    u2.name `to`
from gifts g
join users u1 on g.`to` = u1.id
join users u2 on g.`from` = u2.id
where u1.id = 1;