1到多个关系表连接SQL

时间:2018-05-19 16:57:18

标签: sql join

我想要加入两个表。我想我应该离开加入。表B中有重复的ID,但我希望将详细信息保存在B中,并在B中的详细信息不相同时创建单独的行。

表A:

+----+-------------------------------+------------+
| ID | How Many Times He Has Shopped | TotalSpend |
+----+-------------------------------+------------+
|  1 |                            12 | $100       |
|  2 |                            10 | $152       |
|  3 |                             5 | $30        |
|  4 |                            12 | $300       |
+----+-------------------------------+------------+

表B:

+------+----------+--------------+
| ID   | Income   | TimeMeasured |
| 1    | 20,000   | 2011         |
| 2    | 20,000   | 2011         |
| 3    | 22,000   | 2011         |
| 4    | 20,000   | 2011         |
| 1    | 30,000   | 2012         |
| 2    | 33,000   | 2012         |
| 3    | 20,000   | 2012         |
| 4    | 40,000   | 2012         |
+------+----------+--------------+

我想创建一个如下所示的表:

+--------------------------+-------------+------------------------------+----------+--------------+-----+
| HowManyTimesHeHasShopped |  TotalSpend |  HowManyPeopleFitinCategory  |  income  | TimeMeasured |     |
+--------------------------+-------------+------------------------------+----------+--------------+-----+
|                       12 | 400        |                            2 | 20,000  |         2011 |     |
|                       10 | 152        |                            1 | 20,000  |         2011 |     |
|                        5 | 30         |                            1 | 22,000  |         2011 |     |
|                       12 | 100        |                            1 | 30,000  |         2012 |     |
|                      10  | 152        |                            1 | 33,000   |         2012 |     |
|                       5  | 30         |                            1 | 20,000   |         2012 |     |
|                      12  | 300        |                            1 | 40,000   |         2012 |     |
+--------------------------+-------------+------------------------------+----------+--------------+-----+

1 个答案:

答案 0 :(得分:0)

似乎只是分组和一些聚合函数的问题。

下面的示例SQL在MySql 5.6中进行了测试

设置测试数据

create table TableA_Shoppingsprees (ID INT, HowManyTimesHeHasShopped INT, TotalSpend DECIMAL(15,3));
insert into TableA_Shoppingsprees (ID, HowManyTimesHeHasShopped, TotalSpend) values 
(1,12,100.0), (2,10,152.0), (3,5,30.0), (4,12,300.0);

create table TableB_Earnings (ID INT, Income DECIMAL(15,3), TimeMeasured DECIMAL(4,0));
insert into TableB_Earnings (ID, Income, TimeMeasured) values
(1, 20.0, 2011), (2, 20.0, 2011), (3, 22.0, 2011), (4, 20.0, 2011),
(1, 30.0, 2012), (2, 33.0, 2012), (3, 20.0, 2012), (4, 40.0, 2012);

<强>查询:

select 
 HowManyTimesHeHasShopped,
 sum(TotalSpend) as TotalSpend,
 count(b.ID) as HowManyPeopleFitinCategory,
 Income,
 TimeMeasured
from TableB_Earnings b
join TableA_Shoppingsprees a on (a.ID = b.ID)
group by TimeMeasured, HowManyTimesHeHasShopped, Income 
order by TimeMeasured, min(a.ID);