簡體   English   中英

mysql從不同的表中選擇2行

[英]mysql select sum 2 rows from different tables

我一直試圖找到從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   |
+-----------------+-----------------+-----------------+

前三個總數減去第二個三個總數。

我嘗試過加入,但我真的很難找到合適的結果。

任何建議將不勝感激。

嘗試這個:

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

你只是想這樣做:

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