从MySQL中的两个不同表中减去值

时间:2019-02-12 05:57:34

标签: mysql

我有两个桌子

Meetingyear1
+----+----------+------------+
| id | county   | attendance |
+----+----------+------------+
|  1 | Nyeri    |        100 |
|  2 | Murang'a |         50 |
|  3 | Kiambu   |        200 |
|  4 | Nairobi  |         50 |
|  5 | Kisumu   |        300 |
|  6 | Nyeri    |         50 |
|  7 | Kisumu   |        400 |
|  8 | Nairobi  |        800 |
|  9 | Kiambu   |         80 |
| 10 | Murang'a |        200 |
+----+----------+------------+

Meetingyear2
+----+----------+------------+
| id | county   | attendance |
+----+----------+------------+
|  1 | Nyeri    |         50 |
|  2 | Murang'a |         78 |
|  3 | Kiambu   |        500 |
|  4 | Nairobi  |       1000 |
|  5 | Kisumu   |          0 |
|  6 | Nyeri    |         12 |
|  7 | Kisumu   |        200 |
|  8 | Nairobi  |         60 |
|  9 | Kiambu   |        100 |
| 10 | Murang'a |          0 |
+----+----------+------------+



select meetingyear1.county,sum(meetingyear1.attendance) as attendance1,sum(meetingyear2.attendance) as attendance2,sum(meetingyear1.attendance-meetingyear2.attendance) as difference from meetingyear1 INNER JOIN meetingyear2 ON meetingyear1.county =meetingyear2.county GROUP BY county;

我得到了

+----------+-------------+-------------+------------+
| county   | attendance1 | attendance2 | difference |
+----------+-------------+-------------+------------+
| Kiambu   |         560 |        1200 |       -640 |
| Kisumu   |        1400 |         400 |       1000 |
| Murang'a |         500 |         156 |        344 |
| Nairobi  |        1700 |        2120 |       -420 |
| Nyeri    |         300 |         124 |        176 |
+----------+-------------+-------------+------------+

预期结果是

COUNTY         ATTENDANCE
NYERI            88
MURANG'A         172
KIAMBU           320
NAIROBI          210
KISUMU           500

3 个答案:

答案 0 :(得分:0)

您可以在下面尝试-使用子查询并加入

select A.county, attendance1-attendance2 as difference from
(
select meetingyear1.county, sum(meetingyear1.attendance) as attendance1
from meetingyear1
group by meetingyear1.county
)A inner join
(
select meetingyear2.county, sum(meetingyear2.attendance) as attendance2
from meetingyear2
group by meetingyear2.county
)B on A.county=B.county

答案 1 :(得分:0)

您需要将UNION这两个表放在一起,将出勤率分成两列(每年一列),然后可以通过SUM county来显示结果:

SELECT county, SUM(year1) AS attendance1, SUM(year2) AS attendance2, SUM(year1) - SUM(year2) AS difference
FROM (SELECT id, county, attendance AS year1, 0 AS year2
      FROM meetingyear1
      UNION ALL
      SELECT id, county, 0 AS year1, attendance AS year2
      FROM meetingyear2) a
GROUP BY county
ORDER BY difference

输出:

county      attendance1     attendance2     difference
Kiambu      280             600             -320
Nairobi     850             1060            -210
Nyeri       150             62              88
Murang'a    250             78              172
Kisumu      700             200             500

Demo on dbfiddle

答案 2 :(得分:0)

SELECT county, CASE WHEN diff_ < 0 THEN diff_ * -1 ELSE diff_ END AS diff_
FROM(
select m1.county, SUM(m1.attendance) - (SELECT SUM(m2.attendance) FROM Meetingyear2 m2 WHERE m1.county = m2.county) diff_
from Meetingyear1 m1
GROUP BY m1.county
)T
ORDER BY diff_