当前位置:  开发笔记 > 编程语言 > 正文

MySQL - 需要帮助来找出多个连接

如何解决《MySQL-需要帮助来找出多个连接》经验,为你挑选了1个好方法。

我正在使用以下查询从用户的表中获取事务.然后我想检索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

事务表列:

transaction_id
tw_id
tw
sender_id
recipient_id
amount
timestamp_insert
timestamp_start timestamp_complete transaction_status

用户表列:

u_id,名字



1> Rowland Shaw..:

您需要加入两次,因此:

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

推荐阅读
pan2502851807
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有