我正在使用以下查询从用户创建的表中获取事务。然后我想检索 sender_id 和 recipient_id 的用户名。但是我似乎只能为 recipient_id 或 sender_id 获取它。任何人都知道我如何同时获得两者。
SELECT us.name, ta.amount, ta.recipient_id, ta.sender_id, ta.timestamp_insert
FROM `transactions` AS ta
JOIN users AS us
ON ta.recipient_id=us.u_id
WHERE ta.sender_id =111111 OR ta.recipient_id = 111111
LIMIT 0 , 10
交易表列:
交易编号
tw_id
tw
发件人编号
收件人编号
数量
时间戳_插入
时间戳_开始
时间戳完成
交易状态
用户表列:
你的身份, 姓名
最佳答案
您需要加入两次,因此:
SELECT ta.amount, ta.recipient_id, ta.sender_id, ta.timestamp_insert, sender.name as Sender, recipient.name as Recipient
FROM `transactions` AS ta
JOIN users AS recipient
ON ta.recipient_id=recipient.u_id
JOIN users AS sender
ON ta.sender_id=sender.u_id
WHERE ta.sender_id =111111 OR ta.recipient_id = 111111
LIMIT 0 , 10
关于sql - MySQL - 需要帮助来找出多个连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/511140/