MySQL Union输出错误

时间:2016-11-27 06:38:53

标签: mysql

我有以下表格。

1) Table, Named "issues_tot"
 +---------+------+------------+
| v_code  | oid  |   amount   |
+---------+------+------------+
|      1  |   2  | 200,000.00 |
|      1  |   3  | 80,000.00  |
|      2  |   1  | 40,000.00  |
|      3  |   2  | 150,000.00 |
+---------+------+------------+

2) Table, Named "cp_tot"
+--------+-----+-----------+
| v_code | oid |  amount   |
+--------+-----+-----------+
|      1 |   2 | 68,000.00 |
|      1 |   3 | 50,000.00 |
|      3 |   2 | 75,000.00 |
+--------+-----+-----------+

3) Table, Named "vote"
+--------+-------------------------+
| v_code |          vote           |
+--------+-------------------------+
|      1 | 001-2-6-3-2502          |
|      2 | 001-1-4-21-2202         |
|      3 | 101-1-2-0-1405          |
+--------+-------------------------+   

4) Table, Named "office"

+-----+----------------------+
| oid |        office        |
+-----+----------------------+
|   1 | Weeraketiya          |
|   2 | Tissamaharama        |
|   3 | District Sec |
+-----+----------------------+

所需的输出如下:

+--------+------------+-----------+------------+
| v_code |   Gross    |    Cut    |    Net     |
+--------+------------+-----------+------------+
|      1 | 200,000.00 | 68,000.00 | 132,000.00 |
|      1 | 80,000.00  | 50,000.00 | 30,000.00  |
|      2 | 40,000.00  | 0.00      | 40,000.00  |
|      3 | 150,000.00 | 75,000.00 | 75,000.00  |
+--------+------------+-----------+------------+

02)我使用以下脚本生成输出

    select `vote`.`vote` AS `vote`,`office`.`office` AS `office`,
    `issues_tot`.`amount` AS `Gross`,
    coalesce(`cp_tot`.`amount`,0) AS `Cut`,
    (`issues_tot`.`amount` - coalesce(`cp_tot`.`amount`,0)) AS `Net` 
    from (((`vote` join `issues_tot` on((`vote`.`v_code` = `issues_tot`.`v_code`))) join 
`office` on((`office`.`oid` = `issues_tot`.`oid`))) left join 
`cp_tot` on((`issues_tot`.`v_code` = `cp_tot`.`v_code`)))

但它会产生以下输出:

+------------+----------------+--------------+-------------+--------------+
|   Vote          |    Office  |    Gross     |     Cut     |     Net      |
+---------------+-------------+--------------+-------------+--------------+
| 001-2-6-3-2502| Tissamaharama  |  200,000.00  |  68,000.00  |132,000.00  |
| 001-2-6-3-2502| Tissamaharama  |  200,000.00  |  50,000.00  | 150,000.00 |
| 001-2-6-3-2502| District Sec   |  80,000.00   |  68,000.00  | 12,000.00  |
| 001-2-6-3-2502| District Sec   |  80,000.00   |  50,000.00  | 30,000.00  |
| 001-1-4-21-2202| Weeraketiya   |  40,000.00   |  -          | 40,000.00  |
| 101-1-2-0-1405 | Tissamaharama |  150,000.00  |  75,000.00  | 75,000.00  |
+------------+-----------------+--------------+-------------+--------------+

我无法理解出了什么问题。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

要获得该输出,您只需加入issues_totcp_tot上的v_codeoid并进行简单计算即可获得Net

select v_code, 
       oid, 
       issues_tot.amount as Gross,
       ifnull(cp_tot.amount,0) as Cut,
       issues_tot.amount - ifnull(cp_tot.amount,0) as Net 
  from issues_tot
  left join cp_tot using (v_code, oid);

sqlfiddle

上查看