【问题标题】:mysql select sum 2 rows from different tablesmysql从不同的表中选择总和2行
【发布时间】:2012-05-24 23:20:08
【问题描述】:

我一直在尝试找到对 2 个不同表中的 2 行求和的正确方法。

我可以使用这两个查询轻松识别我想要求和的行:

select * 
from   vui_trading_review_form_client 
where  month = '201202' and client_id='TOTALS';

+--------+------------+-----------+-----------------+-----------------+-----------------+----------------+----------+
| month  | dt_end     | client_id | amt_balance_GBP | amt_balance_EUR | amt_balance_USD | dt_working_day | order_no |
+--------+------------+-----------+-----------------+-----------------+-----------------+----------------+----------+
| 201202 | 2012-02-29 | TOTALS    |            2    |             3   |               4 | 2012-02-29     |        2 |
+--------+------------+-----------+-----------------+-----------------+-----------------+----------------+----------+


select * 
from   vui_trading_review_form_bank 
where  month = '201202' and provider='TOTALS';

+--------+------------+----------+-----------------+-----------------+-----------------+----------+
| month  | dt_end     | provider | amt_balance_GBP | amt_balance_EUR | amt_balance_USD | order_no |
+--------+------------+----------+-----------------+-----------------+-----------------+----------+
| 201202 | 2012-02-29 | TOTALS   |              1  |               1  |              1 |        3 |
+--------+------------+----------+-----------------+-----------------+-----------------+----------+

我想要实现的是一个如下所示的表格

+-----------------+-----------------+-----------------+
| amt_balance_GBP | amt_balance_EUR | amt_balance_USD |
+-----------------+-----------------+-----------------+
| 1               |          2      |             3   |
+-----------------+-----------------+-----------------+

前 3 个总计减去后三个总计。

我已尝试加入,但我真的很难获得正确的结果。

任何建议将不胜感激。

【问题讨论】:

  • 您的两个查询来自同一个表。我认为这是一个错误?另外,你是加还是减这些值(看起来像减)?
  • 嗨,约翰,抱歉,这是一个错误。我在减法
  • 两个表的名字一样吗? vui_trading_review_form_client?
  • 对不起,这是一个错误,第二张表是 vui_trading_review_form_bank
  • 啊好的,请在下面查看我的答案。您需要使用INNER JOINLEFT JOIN

标签: mysql join sum rows


【解决方案1】:

试试这个:

SELECT  (a.amt_balance_GBP - b.amt_balance_GBP) Total_GBP,
        (a.amt_balance_EUR - b.amt_balance_EUR) Total_EUR,
        (a.amt_balance_USD - b.amt_balance_USD) Total_USD
FROM vui_trading_review_form_client a INNER JOIN vui_trading_review_form_bank b
        on (a.Client_ID = b. provider) AND
           (a.Month = b.Month)
WHERE a.month = '201202' and b.provider='TOTALS';

或者如果它不符合您的需求,请尝试使用LEFT JOIN

SELECT  (a.amt_balance_GBP - COALESCE(b.amt_balance_GBP,0)) Total_GBP,
        (a.amt_balance_EUR - COALESCE(b.amt_balance_EUR,0)) Total_EUR,
        (a.amt_balance_USD - COALESCE(b.amt_balance_USD,0)) Total_USD
FROM vui_trading_review_form_client a LEFT JOIN vui_trading_review_form_bank b
        on (a.Client_ID = b. provider) AND
           (a.Month = b.Month)
WHERE a.month = '201202' and b.provider='TOTALS';

【讨论】:

  • 谢谢,这几乎可行。尽管 where month = '201202' and provider='TOTALS'; 但它忽略了 where 子句当我尝试添加它时,我得到一个模棱两可的结果
  • 是的,因为您需要指定应该在哪里分配列的表。尝试添加表的别名,这样您就不会出现模棱两可的列错误。
  • 好的,谢谢,当我运行查询时,我收到 3 个警告,并且生成的表将所有三个值都设为 0。b.amt_balance_GBP 旁边的 0 是什么意思?
  • 你的意思是COALESCE()?它是一个将 null 转换为有效值的函数,例如。 0.
【解决方案2】:

你只是想这样做吗:

select (a.amt_balance_GBP  - b.amt_balance_GBP) as GBP, 
   (a.amt_balance_EUR - b.amt_balance_EUR) as EUR,
   (a.amt_balance_USD - b.amt_balance_USD) as USD
from  vui_trading_review_form_client a, vui_trading_review_form_bank b
where a.month = '201202' and a.client_id ='TOTALS' and
      a.month = b.month and a.client_id = b.provider

【讨论】:

  • 感谢您的回复,是的,这看起来是我想要做的。但是,当我运行它时,会出现语法错误?
  • 我有一个错字,这应该可以在 Sybase ASE 或 SQL Server 中工作,但我不确定其他人。
  • 谢谢,我又遇到了与@johntotetwoo 建议相同的问题 - 所有总和都等于 0。也许是因为我使用的是 mysql
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-05
  • 1970-01-01
  • 2011-08-26
  • 2012-08-09
  • 1970-01-01
相关资源
最近更新 更多