Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 当一个表中的一条记录引用另一个表中的两条记录时,如何连接两个相关表?_Mysql_Sql_Relational Database - Fatal编程技术网

Mysql 当一个表中的一条记录引用另一个表中的两条记录时,如何连接两个相关表?

Mysql 当一个表中的一条记录引用另一个表中的两条记录时,如何连接两个相关表?,mysql,sql,relational-database,Mysql,Sql,Relational Database,我有一个带有发送者id和接收者id字段的事务表,还有一个带有用户id first\u name last\u name等的用户表 我想从事务中查询数据,并从用户中加入详细信息 问题是我需要带上发送者和接收者的名字和姓氏 SELECT t.* u.* FROM transactions t, users u WHERE t.sender_id = u.user_id OR t.receiver_id = u.user_id 这当然不是一个解决方案,因为我无法知道结果集中的发送者/接收者是谁,但它

我有一个带有发送者id和接收者id字段的事务表,还有一个带有用户id first\u name last\u name等的用户表

我想从事务中查询数据,并从用户中加入详细信息 问题是我需要带上发送者和接收者的名字和姓氏

SELECT t.* u.*
FROM transactions t, users u
WHERE t.sender_id = u.user_id OR t.receiver_id = u.user_id
这当然不是一个解决方案,因为我无法知道结果集中的发送者/接收者是谁,但它有助于说明问题

有什么想法吗? 谢谢
Ajar

您需要在表
事务中两次联接表
用户
,因为事务中有两列依赖于表
用户

SELECT  a.*,
        b.firstName as SenderName,
        c.FirstName as RecieverName
FROM    transactions a
        INNER JOIN users b
            ON a.sender_ID = b.user_ID
        INNER JOIN users c
            ON a.reciever_id = c.user_ID

您需要在表
事务中两次联接表
用户
,因为事务中有两列依赖于表
用户

SELECT  a.*,
        b.firstName as SenderName,
        c.FirstName as RecieverName
FROM    transactions a
        INNER JOIN users b
            ON a.sender_ID = b.user_ID
        INNER JOIN users c
            ON a.reciever_id = c.user_ID