多表查询,总和,一对多

时间:2019-06-14 13:07:56

标签: mysql

我有许多MySQL表,并且想要将每个表的总和放入单个结果集中。我正在简化表格,但是我想如何显示数据:

+--------------+-------------------+----------------+
| CustomerName | Transactions(SUM) | Additions(SUM) |
+--------------+-------------------+----------------+
| Customer1    | 8                 | 6              |
+--------------+-------------------+----------------+
| Customer2    | 24                | 4              |
+--------------+-------------------+----------------+

每个客户都有多个站点,每个站点都有多个交易和添加项。我们不需要按站点进行细分,但是需要获取当月/上个月等每个客户的所有交易和增加的总金额。

Table 1: Customers
id, CustomerName

Table 2: Sites    
id, site, CustomerID

Table 3: Transactions
site, price, billingMonth

Table 4: Additions
site, price, date

带有一些基本数据的示例表结构。对于我想要实现的目标,日期内容可以忽略。它只是增加了不必要的复杂性。

/*Table structure for table `additions` */

CREATE TABLE `additions` (
  `site` int(3) DEFAULT NULL,
  `price` int(3) DEFAULT NULL,
  `date` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

/*Data for the table `additions` */

insert  into `additions`(`site`,`price`,`date`) values 
(1,2,'2019-06-14'),
(1,3,'2019-06-14'),
(2,1,'2019-06-14'),
(3,3,'2019-06-14'),
(4,1,'2019-06-14');

/*Table structure for table `customers` */

CREATE TABLE `customers` (
  `id` int(3) NOT NULL AUTO_INCREMENT,
  `CustomerName` char(10) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;

/*Data for the table `customers` */

insert  into `customers`(`id`,`CustomerName`) values 
(1,'Customer1'),
(2,'Customer2');

/*Table structure for table `sites` */

CREATE TABLE `sites` (
  `id` int(3) NOT NULL AUTO_INCREMENT,
  `site` char(10) DEFAULT NULL,
  `customerID` int(3) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4;

/*Data for the table `sites` */

insert  into `sites`(`id`,`site`,`customerID`) values 
(1,'Location1',1),
(2,'Location2',1),
(3,'Location3',2),
(4,'Location4',2);

/*Table structure for table `transactions` */

CREATE TABLE `transactions` (
  `site` int(3) DEFAULT NULL,
  `price` int(3) DEFAULT NULL,
  `billingMonth` char(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

/*Data for the table `transactions` */

insert  into `transactions`(`site`,`price`,`billingMonth`) values 
(1,1,'Jun-2019'),
(2,3,'Jun-2019'),
(3,23,'Jun-2019'),
(4,1,'Jun-2019'),
(1,2,'Jun-2019'),
(2,2,'Jun-2019');

我正在尝试的查询如下:

SELECT `customers`.`CustomerName`, SUM(`transactions`.`price`), SUM(`additions`.`price`)
FROM `customers`
JOIN `sites` ON `customers`.`id` = `sites`.`customerID`
JOIN `transactions` ON `sites`.`id` = `transactions`.`site`
JOIN `additions` ON `sites`.`id` = `additions`.`site`
GROUP BY `customers`.`CustomerName`

但是这将返回错误的结果:

+--------------+-------------------+----------------+
| CustomerName | Transactions(SUM) | Additions(SUM) |
+--------------+-------------------+----------------+
| Customer1    | 11                | 12             |
+--------------+-------------------+----------------+
| Customer2    | 24                | 4              |
+--------------+-------------------+----------------+

1 个答案:

答案 0 :(得分:1)

也许可以帮助

{
 "argv": [
  "C:\\Anaconda3\\python.exe",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python 3",
 "language": "python"
}