汇总,分组合并和加入3个表。

时间:2018-11-09 10:13:54

标签: mysql

我正在开发一种用于管理客户和付款数据的工具。

我使用MySQL并具有以下表格:客户和付款:

customers:
ID | invoiceID | supreme_invoiceID
1   123         a123
2   124         a123
3   103         a103
4   110         a110

payments:
ID | supreme_invoiceID | amount | date
1   a123                10         10.10.2010
2   a103                105         10.11.2017
3   a123                5           11.10.2010

我的结果应如下所示:

view_complete:
ID | supreme_invoideID | number_invoices | GROUP_CONCAT(invoiceID) | SUM(payments.amount) | GROUP_CONCAT(payments.amount)
1   a123                 2                  123;124                 15                      10;15

不幸的是,我无法将其直接放入一张桌子。相反,我创建了2个视图并分别查询付款表以获取有关付款的汇总数据。

首先,我创建一个辅助视图:

CREATE VIEW precomplete as
SELECT *, COUNT(supreme_invoiceID) as number_invoices FROM customers
GROUP BY supreme_invoiceID;

然后,第二个:

Then I take a second VIEW
CREATE VIEW complete AS 
SELECT precomplete.*, SUM(payments.amount)
LEFT JOIN payments p ON precomplete.supreme_invoiceID = p.supreme_invoiceID
GROUP BY precomplete.supreme_invoiceID;

以及在其他查询中收到的串联值。但是我希望在一个查询中全部接收数据,并且希望没有这种视图层次结构。即使只有很少的条目,PhpMyAdmin加载视图的速度也已经相当慢。

非常感谢您的帮助。

谢谢!

1 个答案:

答案 0 :(得分:0)

db设计强制采用一种方法,该方法分别构建聚合,以避免在例如加入公共字段之前出现重复

drop table if exists c,p;
create table c(ID int, invoiceID int, supreme_invoiceID varchar(4));
insert into c values
(1 ,  123 ,        'a123'),
(2 ,  124 ,        'a123'),
(3,  103 ,        'a103'),
(4 ,  110 ,        'a110');

create table p(ID int, supreme_invoiceID varchar(4), amount int, date varchar(10));
insert into p values
(1 ,  'a123'   ,             10   ,      '10.10.2010'),
(2 ,  'a103'   ,             105  ,      '10.11.2017'),
(3 ,  'a123'   ,             5    ,      '11.10.2010');

select c.*,p.*
from
(select min(c.id) minid,count(*) nofinvoices,group_concat(c.invoiceid) gciid, max(supreme_invoiceid) maxsid
from c
group by supreme_invoiceid
) c
join
(select group_concat(supreme_invoiceid) gcsid, sum(amount),group_concat(amount),max(supreme_invoiceid) maxsid
from p
group by supreme_invoiceid
) p
on p.maxsid = c.maxsid
order by minid
;

+-------+-------------+---------+--------+-----------+-------------+----------------------+--------+
| minid | nofinvoices | gciid   | maxsid | gcsid     | sum(amount) | group_concat(amount) | maxsid |
+-------+-------------+---------+--------+-----------+-------------+----------------------+--------+
|     1 |           2 | 123,124 | a123   | a123,a123 |          15 | 10,5                 | a123   |
|     3 |           1 | 103     | a103   | a103      |         105 | 105                  | a103   |
+-------+-------------+---------+--------+-----------+-------------+----------------------+--------+
2 rows in set (0.15 sec)

非常类似于您的视图方法。请注意,客户表中似乎没有客户