2016-11-27 109 views
0

我有以下表格。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 | 
+------------+-----------------+--------------+-------------+--------------+ 

I can not und想知道发生了什么事。谁能帮我?

+0

看到它请格式化查询为可读一个(制动成几行,缩进等等)。 – FDavidov

回答

0

要获得输出,你只需要加入issues_totcp_totv_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

+0

@ Andreas Wedebrand。但是,issues_tot表包含的行数多于cp_tot表。因此,上面的脚本并没有解决我的问题。 – Xtern

+0

例如:-issues_tot表包含10行,而cp_tot包含4行。但是需要输出结果来显示10行,4行将其他6行中的0和行中的10行切割成行。 – Xtern

+0

表cp_tot应该被编辑 – Xtern