MySQL-如果我使用更多的左连接,为什么查询不会对相同类型的数据求和?

MySQL-如果我使用更多的左连接,为什么查询不会对相同类型的数据求和?,mysql,sql,Mysql,Sql,我的架构和示例数据集: CREATE TABLE `bsbi_hedge_fund` ( `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, `fund_name` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL, `abn_number` varchar(13) COLLATE utf8_unicode_ci DEFAULT NULL, `parent_company_id` i

我的架构和示例数据集:

CREATE TABLE `bsbi_hedge_fund` (
  `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `fund_name` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `abn_number` varchar(13) COLLATE utf8_unicode_ci DEFAULT NULL,
  `parent_company_id` int(11) DEFAULT NULL,
  `email` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `contact_no` varchar(16) COLLATE utf8_unicode_ci DEFAULT NULL,
  `address_one` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `address_two` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `city_name` varchar(60) COLLATE utf8_unicode_ci DEFAULT NULL,
  `state_name` varchar(60) COLLATE utf8_unicode_ci DEFAULT NULL,
  `zip_code` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
  `country_tbl_id` int(11) DEFAULT NULL,
  `status` varchar(10) COLLATE utf8_unicode_ci DEFAULT 'active',
  `created_by` int(11) DEFAULT NULL,
  `created_date` datetime DEFAULT NULL,
  `modified_by` int(11) DEFAULT NULL,
  `modified_date` datetime DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `bsbi_hedge_fund`
--

INSERT INTO `bsbi_hedge_fund` (`id`, `fund_name`, `abn_number`, `parent_company_id`, `email`, `contact_no`, `address_one`, `address_two`, `city_name`, `state_name`, `zip_code`, `country_tbl_id`, `status`, `created_by`, `created_date`, `modified_by`, `modified_date`) VALUES
(1, 'iCAP Hedge Funds', '53616271062', 1, 'icaptrading@gmail.com', '0425175887', '68 Roy Marika Street', '', 'Bonner', 'ACT', '2914', 12, 'active', 1, '2020-02-20 07:02:51', NULL, NULL);


CREATE TABLE `bsbi_company` (
  `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `company_name` varchar(60) COLLATE utf8_unicode_ci DEFAULT NULL,
  `abn_number` varchar(13) COLLATE utf8_unicode_ci DEFAULT NULL,
  `acn_number` varchar(13) COLLATE utf8_unicode_ci DEFAULT NULL,
  `email` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `contact_no` varchar(16) COLLATE utf8_unicode_ci DEFAULT NULL,
  `address_one` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `address_two` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `city_name` varchar(60) COLLATE utf8_unicode_ci DEFAULT NULL,
  `state_name` varchar(60) COLLATE utf8_unicode_ci DEFAULT NULL,
  `zip_code` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
  `country_tbl_id` int(11) DEFAULT NULL,
  `status` varchar(10) COLLATE utf8_unicode_ci DEFAULT 'active',
  `created_by` int(11) DEFAULT NULL,
  `created_date` datetime DEFAULT NULL,
  `modified_by` int(11) DEFAULT NULL,
  `modified_date` datetime DEFAULT NULL,
  `allocated_money` float(17,5) DEFAULT '0.00000',
  `hedge_fund_allocation` float(17,5) NOT NULL,
  `investment_class_allocation` float(17,5) NOT NULL,
  `hedge_fund_money` float(10,3) DEFAULT '0.000',
  `inv_class_money` float(10,3) DEFAULT '0.000'
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `bsbi_company`
--

INSERT INTO `bsbi_company` (`id`, `company_name`, `abn_number`, `acn_number`, `email`, `contact_no`, `address_one`, `address_two`, `city_name`, `state_name`, `zip_code`, `country_tbl_id`, `status`, `created_by`, `created_date`, `modified_by`, `modified_date`, `allocated_money`, `hedge_fund_allocation`, `investment_class_allocation`, `hedge_fund_money`, `inv_class_money`) VALUES
(1, 'Investment and Capital Growth for Australian Professional ( ', '53616271062', '2123', 'abc@gmail.com', '2343', '68 Roy Marika Street', '', 'Bonner', 'ACT', '2914', 12, 'active', 1, '2020-02-20 07:01:26', 1, '2020-03-07 12:13:53', 22847.00000, 0.00000, 0.00000, 20000.000, 19000.000);


CREATE TABLE `bsbi_hedge_fund_journal` (
  `id` int(11) NOT NULL,
  `company_id` int(11) DEFAULT NULL,
  `hedge_fund_id` int(11) DEFAULT NULL,
  `amount` float(10,3) DEFAULT NULL,
  `tran_type` varchar(7) COLLATE utf8_unicode_ci DEFAULT NULL,
  `created_by` int(11) DEFAULT NULL,
  `created_date` datetime DEFAULT NULL,
  `modified_by` int(11) DEFAULT NULL,
  `modified_date` datetime DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `bsbi_hedge_fund_journal`
--

INSERT INTO `bsbi_hedge_fund_journal` (`id`, `company_id`, `hedge_fund_id`, `amount`, `tran_type`, `created_by`, `created_date`, `modified_by`, `modified_date`) VALUES
(1, 1, 1, 20000.000, 'credit', 1, '2020-03-07 11:45:40', NULL, NULL);

CREATE TABLE `bsbi_investment_allocation_class_journal` (
  `id` int(11) NOT NULL,
  `hedge_fund_id` int(11) DEFAULT NULL,
  `investment_class_id` int(11) DEFAULT NULL,
  `amount` float(10,3) DEFAULT NULL,
  `tran_type` varchar(7) COLLATE utf8_unicode_ci DEFAULT NULL,
  `created_by` int(11) DEFAULT NULL,
  `created_date` datetime DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `bsbi_investment_allocation_class_journal`
--

INSERT INTO `bsbi_investment_allocation_class_journal` (`id`, `hedge_fund_id`, `investment_class_id`, `amount`, `tran_type`, `created_by`, `created_date`) VALUES
(1, 1, 1, 18000.000, 'credit', 1, '2020-03-07 11:46:09'),
(2, 1, 1, 2000.000, 'credit', 1, '2020-03-07 12:12:02'),
(3, 1, 1, 1000.000, 'debit', 1, '2020-03-07 12:13:53');
…和同样的小提琴():

我编写了以下查询以从数据库中获取一些数据:

SELECT
hf.*, com.company_name company_name, com.id com_id,
IFNULL( SUM( hfc.amount ), 0 ) hedge_credit, IFNULL( SUM( hfd.amount ), 0 ) hedge_debit,
IFNULL( SUM( cjc.amount ), 0 ) class_credit, IFNULL( SUM( cjd.amount ), 0 ) class_debit

FROM bsbi_hedge_fund hf

INNER JOIN bsbi_company com ON hf.parent_company_id = com.id

LEFT JOIN bsbi_hedge_fund_journal hfc ON (hfc.hedge_fund_id = hf.id AND hfc.tran_type='credit')
LEFT JOIN bsbi_hedge_fund_journal hfd ON ( hfd.hedge_fund_id = hf.id AND hfd.tran_type = 'debit' )
LEFT JOIN bsbi_investment_allocation_class_journal cjc ON (cjc.hedge_fund_id = hf.id AND cjc.tran_type = 'credit' )
LEFT JOIN bsbi_investment_allocation_class_journal cjd ON (cjd.hedge_fund_id = hf.id AND cjd.tran_type = 'debit' )
ORDER BY hf.id ASC
以下是此查询的输出:

我面临的问题是:对冲信贷价值是20000,但它显示40000!类似地,class_debit1000,但显示为2000

在做了一些研究和开发之后,我发现:bsbi\u投资\u分配\u分类\u日记账表中有两个条目具有tran\u type=credit,但不知什么原因,我的查询的下一行获取两行,而不是求和值(我担心如果其他左连接也有多条相同类型的记录,那么同样的事情也会发生在他们身上!)

如果我将GROUPBY子句添加到上述查询中,则得到:

请注意,class_credit有两个不同的值,应该求和,但由于未知原因不能求和

有谁能告诉我,在我提出这个问题的问题中,到底哪里出了问题?

-感谢

联接生成符合联接条件的所有可能的行组合。因为您仅使用
hfc.hedge\u fund\u id=hf.id
联接表,所以您正在复制行。如果总共有三个事务,则每个事务都将增加三倍


您需要从包含事务的表中选择更多数据,以保持行的唯一性:时间戳或事务ID。我通常会先选择所需的唯一行,然后将非唯一数据(名称、国家等)左键连接到每一行。

我从以下方面获得了解决方案:


如果有人面临确切类型的问题,那么他们可以检查此解决方案:

@草莓:如果要重现问题,请检查此项:表“db_9_9cd46d.bsbi_country”不存在我不知道此查询如何运行。@草莓为什么要复制此查询而不是运行SQLFIDLE上可用的查询?我已删除所有查询与国家表相关。UPD:我也更新了这里的查询。@草莓,顺便说一下,兄弟,我用这个得到了解决方案:谢谢你的帮助
LEFT JOIN bsbi_investment_allocation_class_journal cjc ON (cjc.hedge_fund_id = hf.id AND cjc.tran_type = 'credit' )