sql - 使用内连接来连接 3 个表?

标签 sql ms-access

如何连接3个表?

select * 
from tblcustomer as cust 
    inner join (tblamountdetails as det on det.[CustomerID]=cust.[CustomerID]) 
    inner join (select cash.AccountID, sum(Amount) as [Paid Amount] 
                from tblcashdetails as cash 
                group by cash.AccountID) as cash2 
        on cash2.AccountID=det.AccountID

表格格式:

1) cutomertable:
    customerid | customername | Address | phone
        1            arun       palani    1212112221
        2            aaa        sssss    123123123

2)Amountdetailtable:
    AccountID | customerid | Total amount | Daily Amount
        1            1            12000        120

3)cashtable : 
    AccountID |   customerid | amount(given day by day)
        1            1                120
        1            1                120

最后我想要这样......

    customerid | customername |AccountID| totalamount | daily amount | amount(given)
        1            arun          1        12000            120         240(this is sum of amount in table 3 where custid=1)

最佳答案

select 
    cust.customerid,
    cust.customername,
    amt.AccountID,
    amt.[Total amount],
    amt.[Daily Amount],
    t.amountgiven
from cutomertable cust
inner join Amountdetailtable amt on cust.customerid=amt.customerid
inner join (select SUM(amount) amountgiven,customerid from cashtable group by customerid)t 
on t.customerid=cust.customerid

<强> SQL FIDDLE

Fiddle 花了很多时间

关于sql - 使用内连接来连接 3 个表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16707663/

相关文章:

vb.net - OleDbCommandBuilder 创建导致 "syntax error"的 SQL 语句

sql - MySQL 中的子字符串

sql - sql server 2008 中的 12 个月销售和最大销售

java mysql准备语句

C# 版本的 SQL LIKE

mysql - 如何在SQL中获取超过特定数量的第一行

sql-server - 加载包含链接的 sql server 表的 mdb 时,ms access 崩溃

sql - 如何消除 MS Access 2003 中具有唯一 ID 的重复条目?

sql - 如何使用多个 INNER JOIN 加速查询

mysql - 如何将自定义记录添加到包含链接表的查询中?