【问题标题】:MySQL - Need help to figure out multiple joinsMySQL - 需要帮助来确定多个连接
【发布时间】:2009-02-04 12:28:13
【问题描述】:

我正在使用以下查询从与用户进行的表中获取事务。然后我想检索 sender_id 和 recipient_id 的用户名。但是,我似乎只能为收件人 ID 或发件人 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
发件人ID
收件人ID
金额
时间戳插入
时间戳开始 时间戳_完成 交易状态

用户表列:

u_id, 名字

【问题讨论】:

    标签: sql mysql


    【解决方案1】:

    您需要加入两次,因此:

    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
    

    【讨论】:

      猜你喜欢
      • 2016-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-01
      相关资源
      最近更新 更多