SQL复杂联接

huangapple go评论73阅读模式
英文:

sql complex join

问题

以下是您要翻译的部分:

我有三个相关的表格,我想使用连接检索数据:tbl_accounts 包含用户的帐户详细信息,tbl_users 包含用户的个人详细信息,tbl_t_records 包含所有用户的交易详细信息。

在 tbl_accounts 中,我有一个外键列 "holder",用于将其与 tbl_users 进行关联,而在 tbl_t_records 中,我有外键列 "oaccount" 和 "daccount",分别链接到 tbl_accounts,但 "oaccount" 存储了交易发起的帐户详细信息,而 "daccount" 存储了交易目标帐户的详细信息。

这是 tbl_accounts 表格的摘录:
SQL复杂联接

这是 tbl_users 的摘录:
SQL复杂联接

这是 tbl_t_records 的摘录:
SQL复杂联接

我想创建一个查询,使用连接返回发送者、接收者和交易ID的名称。
到目前为止,这是我的尝试:

SELECT tbl_t_records.id as 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

这并未提供目标帐户的详细信息,而只呈现了发送方的数据,如下所示:
SQL复杂联接

我做错了什么?

英文:

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.

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 destined

here is the snippet fro the tbl_accounts table
SQL复杂联接

Here is the one for tbl_users
SQL复杂联接

This is the one for tbl_t_records
SQL复杂联接

I would like to create a query that return the name of a sender, receiver and the transaction id using joins.
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
SQL复杂联接
What am I doing wrong?

答案1

得分: 1

只需多次使用join操作,每个名称都需要一个单独的集合:

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 just need multiple joins -- a separate set for each name:

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;

答案2

得分: 1

以下是翻译好的代码部分:

select 
	r.id 交易ID,
    concat(u1.lname, u1.othername) 发件人, 
    concat(u2.lname, u2.othername) 收件人, 
    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
英文:

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). Another series of joins starts from daccount and brings the related user data (aliases a2/u2).

huangapple
  • 本文由 发表于 2020年1月6日 23:18:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614597.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定