简体   繁体   English

sql复杂连接

[英]sql complex join

I have the three related tables that i would like to retrieve data from using joins: tbl_accounts containing the account details of users, tbl_users containing the personal details of users and tbl_t_records that contains the transaction details of all users.我有三个相关的表,我想使用联接从中检索数据:包含用户帐户详细信息的 tbl_accounts、包含用户个人详细信息的 tbl_users 和包含所有用户交易详细信息的 tbl_t_records。

In the tbl_accounts i have a foreign key column holder that helps link it to the tbl_users and in the tbl_t_records I have the foreign key columns oaccount and daccount each linking to tbl_accounts but the oaccount stores the details for the account from which the transaction originated while daccount stores the details for the account to which the transaction is destinedtbl_accounts 中,我有一个外键列持有者,可以帮助将其链接到tbl_users ,在tbl_t_records 中,我有外键列oaccountdaccount,每个列都链接到tbl_accounts,oaccount存储了在daccount期间发起交易的帐户的详细信息存储交易目的地帐户的详细信息

here is the snippet fro the tbl_accounts table这是 tbl_accounts 表的片段tbl_accounts 屏幕截图

Here is the one for tbl_users这是 tbl_users 的一个在此处输入图片说明

This is the one for tbl_t_records这是 tbl_t_records 的一个在此处输入图片说明

I would like to create a query that return the name of a sender, receiver and the transaction id using joins.我想创建一个查询,该查询使用连接返回发送者、接收者和交易 ID 的名称。 so far this is what I have tried到目前为止,这是我尝试过的

    SELECT tbl_t_records.id transaction_id,
            CONCAT(tbl_users.lname,tbl_users.othername)as sender, 
            amount,
            CONCAT(tbl_users.lname,tbl_users.othername)as receiver 
    FROM tbl_t_records
        INNER JOIN tbl_accounts on oaccount = tbl_accounts.id 
        INNER JOIN tbl_users ON tbl_accounts.holder = tbl_users.id 

this does not give me the details of the destination account rather it only presents the data fro the sender that it uses as the same for the reciever as shown here这不会给我目标帐户的详细信息,而只是显示发件人的数据,它与收件人的数据相同,如此处所示在此处输入图片说明 What am I doing wrong?我究竟做错了什么?

You just need multiple join s -- a separate set for each name:您只需要多个join s - 每个名称的单独集合:

SELECT r.*,
       CONCAT(uo.lname, uo.othername) as sender,
       CONCAT(ud.lname, ud.othername) as receiver
FROM tbl_t_records r JOIN
     tbl_accounts ao
     ON r.oaccount = ao.id JOIN
     tbl_users uo
     ON ao.holder = uo.id JOIN
     tbl_accounts ad
     ON r.daccount = ad.id JOIN
     tbl_users ud
     ON ad.holder = ud.id;

You can join several times, as follows:您可以多次加入,如下所示:

select 
    r.id transaction_id,
    concat(u1.lname, u1.othername) sender, 
    concat(u2.lname, u2.othername) receiver, 
    r.amount
from tbl_t_records r
inner join tbl_accounts a1 on a1.id = r.oaccount
inner join tbl_users u1 on u1.id = a1.holder
inner join tbl_accounts a2 on a2.id = r.daccount
inner join tbl_users u2 on u2.id = a2.holder

This query starts from the transaction table and then follows the relationships related to oaccount through tbl_accounts (aliased a1 ) to tbl_users (aliased u1 ).此查询从事务表开始,然后通过tbl_accounts (别名a1 )到tbl_users (别名u1 )遵循与oaccount相关的关系。 Another series of joins starts from daccount and brings the related user data (aliases a2/u2 ).另一系列连接从daccount开始,并带来相关的用户数据(别名a2/u2 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM