Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MYSQL数据库求和函数_Mysql - Fatal编程技术网

MYSQL数据库求和函数

MYSQL数据库求和函数,mysql,Mysql,我创建了以下查询来联接表registration、expectedamaount和contribution,其中我想从SUM contribution表中减去SUM FORM amount表,但得到的结果是错误的 +----------------+ | Tables_in_test | +----------------+ | contribution | | expectedamount | | registration | +----------------+ registrat

我创建了以下查询来联接表registration、expectedamaount和contribution,其中我想从SUM contribution表中减去SUM FORM amount表,但得到的结果是错误的

+----------------+
| Tables_in_test |
+----------------+
| contribution   |
| expectedamount |
| registration   |
+----------------+

registration table
+----+--------+-------------+
| id | reg_no | fullname    |
+----+--------+-------------+
|  1 | TTI001 | JOHN JAMES  |
|  2 | TTI002 | DAVID CERES |
|  3 | TTI003 | JOYCE LEE   |
|  4 | TTI004 | JOEL MARTIN |
+----+--------+-------------+

espectedamount
+----+--------+---------+---------+---------+
| id | reg_no | number1 | number2 | number3 |
+----+--------+---------+---------+---------+
|  1 | TTI001 |     500 |     500 |     500 |
|  2 | TTI002 |     500 |     500 |     500 |
|  3 | TTI003 |     500 |     500 |     500 |
|  4 | TTI004 |     500 |     500 |     500 |
|  5 | TTI001 |     400 |     400 |     400 |
|  6 | TTI001 |    1000 |    1000 |    1000 |
|  7 | TTI002 |    1000 |    1000 |    1000 |
|  8 | TTI003 |    1000 |    1000 |    1000 |
|  9 | TTI004 |    1000 |    1000 |    1000 |
+----+--------+---------+---------+---------+

contribution table
+----+--------+---------+---------+---------+
| id | reg_no | number1 | number2 | number3 |
+----+--------+---------+---------+---------+
|  1 | TTI001 |     200 |     400 |     600 |
|  2 | TTI002 |     100 |      50 |     250 |
|  3 | TTI001 |     100 |     400 |     400 |
|  4 | TTI002 |     300 |     400 |     600 |
|  5 | TTI003 |     300 |     100 |      50 |
|  6 | TTI004 |      50 |      60 |      40 |
|  7 | TTI004 |     500 |     300 |     400 |
+----+--------+---------+---------+---------+
预期结果

select registration.reg_no
      ,registration.fullname
      ,sum(expectedamount.number1-contribution.number1) as contribution1
      ,sum(expectedamount.number2-contribution.number2) as contribution2
      ,sum(expectedamount.number3-contribution.number3) as contribution3 
FROM registration
INNER JOIN expectedamount ON registration.reg_no = expectedamount.reg_no 
INNER JOIN contribution ON expectedamount.reg_no = contribution.reg_no
GROUP BY reg_no;

+--------+-------------+---------------+---------------+---------------+
| reg_no | fullname    | contribution1 | contribution2 | contribution3 |
+--------+-------------+---------------+---------------+---------------+
| TTI001 | JOHN JAMES  |           700 |           200 |             0 |
| TTI002 | DAVID CERES |           600 |           550 |           150 |
| TTI003 | JOYCE LEE   |           200 |           400 |           450 |
| TTI004 | JOEL MARTIN |           450 |           640 |           560 |
+--------+-------------+---------------+---------------+---------------+

请大家帮忙。

我想你需要这个-

+-------+---------------+---------------+------------------+--------------+
|reg_no | fullname      | contribution1 | contribution2    | contribution3|
+-------+---------------+---------------+------------------+--------------+
|TTI001 | JOHN JAMES    | 1600          | 1100             |   900        |
|TTI002 | DAVID CERES   | 1000          | 950              |   550        |
|TTI003 | JOYCE LEE     | 1200          | 1400             |   1450       |
|TTI004 | JOEL MARTIN   |  950          | 1140             |   1060       |
+-------+---------------+---------------+------------------+--------------+

这就是你错的地方:我的表格如下,请格式化你的问题。如前所述,正确的结果是什么?答案应该是TT01 200-300-500,TT02 100 50 350,TT02,TT03 200 400 450,TT04 50 140 60这工作正常,但当我将其应用于包含大量数据的表时,它工作不正常表中是否还有其他列?我正在使用的表中有许多列,它应该有四个工作正常的列。但是我添加了一些我不理解为什么他们显示的数据不正确当一个人在expectedamount表上有两个或多个条目时会出现问题例如如果TTI001有另一个条目编号1400、编号2400、编号3400 sum在expectedamount表上不起作用这一个完全卡住了
select registration.reg_no
  ,registration.fullname
  ,sum(expected.num1-contrib.num1) as contribution1
  ,sum(expected.num2-contrib.num2) as contribution2
  ,sum(expected.num3-contrib.num3) as contribution3 
FROM registration
INNER JOIN (SELECT reg_no, SUM(number1) num1, SUM(number2) num2, SUM(number3) num3
            FROM expectedamount
            GROUP BY reg_no) expected ON registration.reg_no = expected.reg_no 
INNER JOIN (SELECT reg_no, SUM(number1) num1, SUM(number2) num2, SUM(number3) num3
            FROM contribution
            GROUP BY reg_no) contrib ON expected.reg_no = contrib.reg_no
GROUP BY reg_no;