【发布时间】:2021-08-02 03:54:54
【问题描述】:
我正在尝试从table_a 获取数据,左侧的sum() 加入table_b,另一个左侧的sum() 加入table_c
所以,加入a 和b:
SELECT a.id, a.name, sum(b.quantity)
FROM table_a a
LEFT JOIN table_b b on b.id_table_a = a.id
GROUP BY a.id;
输出
| id | name | sum(b.quantity) |
|---|---|---|
| 1 | test | 14 |
并且,加入a 和c:
SELECT a.id, a.name, sum(c.quantity)
FROM table_a a
LEFT JOIN table_c c on c.id_table_a = a.id
GROUP BY a.id;
输出
| id | name | sum(c.quantity) |
|---|---|---|
| 1 | test | 8 |
当我尝试同时加入a 和b 和c 时出现问题
SELECT a.id, a.name, SUM(b.quantity), SUM(c.quantity)
FROM table_a a
LEFT JOIN table_b b ON b.id_table_a = a.id
LEFT JOIN table_c c ON c.id_table_a = a.id
GROUP BY a.id;
输出
| id | name | sum(b.quantity) | sum(c.quantity) |
|---|---|---|---|
| 1 | test | 28 | 24 |
sum() 乘以连接表中的行数(14x2 和 8x3)
此时的问题是:我怎样才能得到正确的总和?
这是我所期望的:
| id | name | sum(b.quantity) | sum(c.quantity) |
|---|---|---|---|
| 1 | test | 14 | 8 |
现在我正在执行以下操作,但我想知道使用上一个查询是否可以实现相同的结果,也许我在 group by 中遗漏了一些东西?
SELECT
x.*, SUM(c.quantity)
FROM
(SELECT
a.id, a.name, SUM(b.quantity)
FROM
table_a a
LEFT JOIN table_b b ON b.id_table_a = a.id
GROUP BY a.id) x
LEFT JOIN
table_c c ON c.id_table_a = x.id
GROUP BY x.id;
这是一个指向小提琴的链接,位于 SQL 下方
http://sqlfiddle.com/#!9/b40639
CREATE TABLE `table_a` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NULL,
PRIMARY KEY (`id`));
CREATE TABLE `table_b` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`id_table_a` INT UNSIGNED NOT NULL,
`quantity` TINYINT UNSIGNED NULL,
PRIMARY KEY (`id`),
INDEX `fk_table_b_id_table_a1_idx` (`id_table_a` ASC),
CONSTRAINT `fk_table_b_id_table_a1`
FOREIGN KEY (`id_table_a`)
REFERENCES `table_a` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
CREATE TABLE `table_c` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`id_table_a` INT UNSIGNED NOT NULL,
`quantity` TINYINT UNSIGNED NULL,
PRIMARY KEY (`id`),
INDEX `fk_table_c_id_table_a1_idx` (`id_table_a` ASC),
CONSTRAINT `fk_table_c_id_table_a1`
FOREIGN KEY (`id_table_a`)
REFERENCES `table_a` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
INSERT INTO `table_a` (`name`) VALUES ('test');
INSERT INTO `table_b` (`id_table_a`, `quantity`) VALUES ('1', '2');
INSERT INTO `table_b` (`id_table_a`, `quantity`) VALUES ('1', '4');
INSERT INTO `table_b` (`id_table_a`, `quantity`) VALUES ('1', '8');
INSERT INTO `table_c` (`id_table_a`, `quantity`) VALUES ('1', '3');
INSERT INTO `table_c` (`id_table_a`, `quantity`) VALUES ('1', '5');
【问题讨论】: