用mysql连接四个表

时间:2014-09-01 05:08:31

标签: mysql

我有四个表。我想要四个表的内连接结果。我的查询是:

select 
    customer.id,
    customer.name,
    customer.opeing_balance,
    customer.opening_balance_type,
    sum(sale.total) as sum_total,
    sum(receipt.net_amount) as sum_net_amount, 
    sum(sale_return.total_return) as sum_sale_return 
from customer 
    inner join sale on customer.id=sale.cust_id 
    inner join receipt on customer.id=receipt.cust_id 
    inner join sale_return on customer.id=sale_return.cust_id 
group by customer.id

表格结构

customer

id   name   opening_balance    opening_balance_type
26   neena    50                  debit
27   shan     50                  debit


sale

cust_id         total
27               50
27               50
26               60
26               40

receipt

cust_id     net_amount
 27            10
 27            10
 26            50
 26             10

sale_return

cust_id         total_return
 27                10
 27                20
 26                15
 26                20

结果

name opening_balance opening_balance_type sum_total sum_net_amount sum_sale_return
neena 50 debit 100 60 35 shan 50 debit 100 20 30

我想要customer_name,opeing_balance,opening_balance_type,总数之和,net_amount之和,每个客户的total_retun之和。任何人都可以给出任何一个?

4 个答案:

答案 0 :(得分:0)

您正在使用汇总功能,那么您应该使用group by,但您的group by仅限于id列。 您应该对未包含在聚合函数中的所有列进行group by,这意味着:

group by customer.id, customer.name, customer.opeing_balance, customer.opening_balance_type

这是您的查询问题,但我不知道它会解决您的问题。告诉我

答案 1 :(得分:0)

please Try This query
select customer.id,customer.name,customer.opeing_balance,customer.opening_balance_type,sum(sale.total) as sum_total,sum(receipt.net_amount) as sum_net_amount, sum(sale_return.total_return) as sum_sale_return from customer as customer inner join sale as sale on customer.id=sale.cust_id inner join receipt as receipt on customer.id=receipt.cust_id inner join sale_return  as sale_return on customer.id=sale_return.cust_id group by customer.id

答案 2 :(得分:0)

您的sale表格中没有自己的id列,因此您的收据和退货与客户相关联,但与特定销售无关。当您一起加入表格时,您将获得所有收据的Cartesian product以及所有收据和退货。例如,您有一位客户:

id name
26 neena

她有两个销售:

cust_id total
26      60
26      40

她还有两个回报:

cust_id total_return
26      15
26      20

并且这些表的连接产生:

cust_id total total_return
26      60    15
26      60    20
26      40    15
26      40    20

请注意,sale中的每条记录都已与sale_return中的每条记录配对。这是数据库连接的作用,除非您包含限制哪些行可以与哪些其他行配对的连接条件。

您需要在id表格中添加sale列并链接收据并返回特定销售而不仅仅是客户,这样当您加入表格时就可以使用{ {1}}。如果一次销售可能有多个收据,您可能还需要将on sale_return.sale_id = sale.id添加到id,并将每次返回链接到该销售而不是销售。


以下是逐步了解Neena的结果:

首先,你用receipt拉入Neena的记录。 (我忽略了Shan的记录。)最初你只有一行:

from customer

然后你加入id name 26 neena 的销售。这会将单个客户行与具有相同客户ID的所有销售进行配对,因此现在您有两行:

inner join sale on customer.id=sale.cust_id

然后用id name cust_id total 26 neena 26 60 26 neena 26 40 加入她的收据。这将上面两行中的每个与所有具有相同客户ID的收据配对,因此现在您有四个行:

inner join receipt on customer.id=receipt.cust_id

最后,您使用id name cust_id total cust_id net_amount 26 neena 26 60 26 50 26 neena 26 60 26 10 26 neena 26 40 26 50 26 neena 26 40 26 10 加入她的回复。这将四行中的每一行与具有相同客户ID的所有退货配对,因此现在您有八个行:

inner join sale_return on customer.id=sale_return.cust_id

这八行是整个id name cust_id total cust_id net_amount cust_id total_return 26 neena 26 60 26 50 26 15 26 neena 26 60 26 50 26 20 26 neena 26 60 26 10 26 15 26 neena 26 60 26 10 26 20 26 neena 26 40 26 50 26 15 26 neena 26 40 26 50 26 20 26 neena 26 40 26 10 26 15 26 neena 26 40 26 10 26 15 子句的结果,它们是from函数的输入。 sum是60 + 60 + 60 + 60 + 40 + 40 + 40 + 40,即400. sum(sale.total)是50 + 50 + 10 + 10 + 50 + 50 + 10 + 10,这是240,而sum(receipt.net_amount)是15 + 20 + 15 + 20 + 15 + 20 + 15 + 20,这是140。

答案 3 :(得分:0)

好的,试试这个:

select 
    customer.id,
    customer.name,
    customer.opeing_balance,
    customer.opening_balance_type,
    s.total,
    r.net_amount,
    s_r.total_return
from customer INNER JOIN
    (SELECT cust_id,SUM(total) total FROM sale GROUP BY cust_id) s ON s.cust_id = customer.id INNER JOIN
    (SELECT cust_id,SUM(net_amount) net_amount FROM receipt GROUP BY cust_id) r ON r.cust_id = customer.id INNER JOIN
    (SELECT cust_id,SUM(total_return) total_return FROM sale_return GROUP BY cust_id) s_r ON s_r.cust_id = customer.id

SQLFiddle demo here