doujian4752 2016-06-02 13:17
浏览 81
已采纳

用左连接的两个表的mysql查询 - 得到错误的结果

I am joining two table with left join to get sum results but its giving wrong results. here is my

table1
    | id | event | currency | amount |
    |----|-------|----------|--------|
    | 1  | HSA   | USD      | 2000   |
    | 2  | DMME  | USD      | 3000   |
    | 3  | HSI   | INR      | 1500   |
    | 4  | HSI   | INR      | 1500   | 

table2

| id | table1_id | rcvamount | adjamount |
|----|-----------|-----------|-----------|
| 1  | 1         | 1980      | 20        |
| 2  | 2         | 1000      | 180       |
| 3  | 2         | 1500      | 20        |
| 4  | 4         | 1487      | 13        |

here is my query

SELECT
T1.event,T1.currency,
SUM(T1.total) AS Totalvalue,

SUM(T1.received) AS Received, 
(T1.bal) AS balance
FROM (SELECT
      table1.id,
      table1.amount AS total,

      SUM(table2.rcvamount+table2.adjamount) AS received,
      ((table1.amount)- sum(table2.rcvamount+table2.adjamount)) AS bal,
      table1.event,  
      table1.currency
      FROM table1 LEFT JOIN table2 ON table1.id=table2.table1_id 
     group by table1.id)T1
      Group By T1.event,T1.currency

But when i run this query giving wrong results as below.when it comes to third event called HSI does not have any row for id number 3 in table2. Results should be group by event and currency.

| event | currency | Totalvalue | Received | balance |
|-------|----------|------------|----------|---------|
| DMME  | USD      | 3000       | 2700     | 300     |
| HSA   | USD      | 2000       | 2000     | 0       |
| HSI   | INR      | 3000       | 1500     | NULL    |

But actual results should be as follows

| event | currency | Totalvalue | Received | balance |
|-------|----------|------------|----------|---------|
| DMME  | USD      | 3000       | 2700     | 300     |
| HSA   | USD      | 2000       | 2000     | 0       |
| HSI   | INR      | 3000       | 1500     | 1500    |

I am not getting where my query goes wrong .Please help me to sort this.Thank you.

  • 写回答

2条回答 默认 最新

  • doujing5150 2016-06-02 13:54
    关注

    Your are using LEFT JOIN so you will have all first table rows even they havent any match in the second table by the required join.

    In your case in the first table the id=3 have no match in the second table so you get the NULL results. (SELECT * FROM table2 WHERE table1_id =3 will give 0 results).

    To avoid this use INNER JOIN :

    SELECT T1.event, T1.currency, SUM( T1.total ) AS Totalvalue, 
           SUM( T1.received ) AS Received, ( T1.bal) AS balance
        FROM (
         SELECT table1.id, table1.amount AS total, 
         SUM( table2.rcvamount + table2.adjamount ) AS received, 
         ((table1.amount) - sum( table2.rcvamount + table2.adjamount ))AS bal,
         table1.event, table1.currency
         FROM table1
         INNER JOIN table2 ON table1.id = table2.table1_id
        GROUP BY table1.id
       )T1
    
     GROUP BY T1.event, T1.currency
    

    UPDATE :

    try this ,COALESCE() will transform each null to 0

    SELECT T1.event, T1.currency, SUM( T1.total ) AS Totalvalue, SUM( T1.received ) AS Received, (
    T1.bal
    ) AS balance
    FROM (
    
    SELECT table1.id, table1.amount AS total, SUM( COALESCE( table2.rcvamount, 0 ) + COALESCE( table2.adjamount, 0 ) ) AS received, (
    COALESCE( table1.amount, 0 ) - sum( COALESCE( table2.rcvamount, 0 ) + COALESCE( table2.adjamount, 0 ) )
    ) AS bal, table1.event, table1.currency
    FROM table1
    LEFT JOIN table2 ON table1.id = table2.table1_id
    GROUP BY table1.id
    )T1
    GROUP BY T1.event, T1.currency
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?