mysql SQL查询返回双倍总金额的问题

icomxhvb  于 5个月前  发布在  Mysql
关注(0)|答案(1)|浏览(42)

我遇到了一个SQL查询返回不正确的总数的问题。我在这里设置了一个SQL Fiddle来演示这个问题:SQL Fiddle示例。
下面是schema:

CREATE TABLE `work_type_config` (
  `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `project_files` (
  `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `identifier_code` bigint(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `funding_lines` (
  `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `project_file_id` bigint(20) UNSIGNED DEFAULT NULL,
  `estimated_amount` double DEFAULT NULL,
  `notified_amount` double DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`project_file_id`) REFERENCES `project_files` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `works` (
  `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `project_file_id` bigint(20) UNSIGNED DEFAULT NULL,
  `description` text COLLATE utf8mb4_unicode_ci,
  `type_id` bigint(20) UNSIGNED DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`project_file_id`) REFERENCES `project_files` (`id`),
  FOREIGN KEY (`type_id`) REFERENCES `work_type_config` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

字符串
并插入:

INSERT INTO `work_type_config` (`name`) VALUES
('Renovation'),
('Electrical'),
('Plumbing'),
('Painting');

INSERT INTO `project_files` (`identifier_code`) VALUES
(123456789),
(987654321),
(123123123),
(321321321);

INSERT INTO `funding_lines` (`project_file_id`, `estimated_amount`, `notified_amount`) VALUES
(1, 10000.00, 9500.00),
(2, 15000.00, 14500.00),
(2, 3000.00, 500.00),
(3, 5000.00, 4800.00),
(4, 20000.00, 19000.00);

INSERT INTO `works` (`project_file_id`, `description`, `type_id`) VALUES
(1, 'Complete renovation of the kitchen', 1),
(2, 'Installation of new electrical wiring in the living room', 2),
(2, 'Another work', 1),
(3, 'Bathroom plumbing repairs', 3),
(4, 'Repainting of the exterior walls', 4);


我的问题是:

select pf.id, pf.identifier_code, fl.estimated_amount, fl.notified_amount, 
group_concat(distinct w.description separator ', ') as works, wtc.name, 
round(sum(fl.notified_amount),2) AS total_notified

from project_files pf join funding_lines fl on (pf.id = fl.project_file_id)
                      join works w on (pf.id = w.project_file_id)
                      join work_type_config wtc on (w.type_id = wtc.id)
where pf.id = 2
group by pf.id


我希望round(sum(fl.notified_amount),2)AS total_notified的结果是15000(14500 + project_files_id 2的500),但实际上,我得到了30000。每次我在works表中添加一行时,总金额似乎都翻了一番。
有人能帮助我了解为什么会发生这种情况,以及如何解决它?任何见解或建议将不胜感激。
先谢谢你了!

zaqlnxep

zaqlnxep1#

我不确定这是否能达到你想要的效果,每当你遇到这样的基数问题时,你就需要选择你想要丢失的信息,或者有一些丑陋的反规范化。
如果你在加入任何东西之前得到total_amount的总和,project_file_id将变得唯一,你不会得到重复。当然,现在那些max()只是显示任意的最大值,只有总和是有意义的。
https://dbfiddle.uk/IoNjGJrl

select
  pf.id,
  pf.identifier_code,
  fl.estimated_amount,
  fl.notified_amount,
  group_concat(distinct w.description separator ', ') as works,
  wtc.name,
  fl.total_notified AS total_notified
from
  project_files pf
  join (
    select
      project_file_id,
      max(estimated_amount) as estimated_amount,
      max(notified_amount) as notified_amount,
      round(sum(notified_amount), 2) as total_notified
    from
      funding_lines
    group by
      project_file_id
  ) as fl on (pf.id = fl.project_file_id)
  join works w on (pf.id = w.project_file_id)
  join work_type_config wtc on (w.type_id = wtc.id)
where
  pf.id = 2
group by
  pf.id

字符串
| ID|识别码|估计数额|通知金额|作品|名称|total_noticed|
| --|--|--|--|--|--|--|
| 2 | 987654321 | 15000 | 14500 |另一项工作,安装新的电线在客厅|改造|一万五千|

相关问题