简体   繁体   English

mysql从不同的表中选择2行

[英]mysql select sum 2 rows from different tables

I have been trying to find the correct way to sum 2 rows from 2 different tables. 我一直试图找到从2个不同的表中总结2行的正确方法。

I can easily identify the rows I want to sum using these two queries: 我可以使用这两个查询轻松识别我想要求和的行:

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 |
+--------+------------+----------+-----------------+-----------------+-----------------+----------+

What I would like to achieve is a table which looks as follows 我想要实现的是一个表格,如下所示

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

the first 3 totals minus the second three totals. 前三个总数减去第二个三个总数。

I have tried a join but i'm really struggling to get the right result. 我尝试过加入,但我真的很难找到合适的结果。

Any suggestions would be greatly appreciated. 任何建议将不胜感激。

Try this: 尝试这个:

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';

or if it does not fit your needs, try to use LEFT JOIN 或者如果它不符合您的需求,请尝试使用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';

Are you just trying to do this: 你只是想这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM