Add values of 2 tables in MYSQL

0

I have 2 tables with the same data, one from 2016 and another from 2017, I want to make a query that adds the data of both tables to make the total of 2 years.

    
asked by Granedoy 05.01.2018 в 18:53
source

4 answers

1

I assume for the example that each of these parameters correspond to a "Player", it could be any other grouping parameter. The Query would be like this, to add all the Recorded Runs (CA) corresponding to each player.

Query:

SELECT jugador,sum(CA) totalCA // Puedes agregar tantos sum como quieras
FROM
(
    SELECT jugador,CA // Puedes agregar tantos campos como quieras
    FROM 2016_table
    union all
    SELECT jugador,CA // Puedes agregar tantos campos como quieras
    FROM 2017_table
) t
GROUP BY jugador
  

Clarification: For the sum (x) to be coherent, you must have the   parameters in both SELECT of FROM (One of the table 2017 and other   in table 2016, as seen with "CA")

    
answered by 05.01.2018 / 19:07
source
0

In the first table the value of CA is 28 and in the other is 3.

I've echoed this query based on what you suggested to me SELECT sum(CA) totalCA FROM (SELECT CA FROM stats WHERE id=6) as xxx union all (SELECT CA FROM stats2017 WHERE id=6)

and I get this result.

but I do not get the sum ... what's wrong?

    
answered by 06.01.2018 в 05:14
0

PROBLEM RESOLVED. I never set the alias t of the example.

 SELECT sum(CA) CA from (SELECT CA from stats WHERE id=6 union all SELECT CA FROM stats2017 where id = 6) x
    
answered by 06.01.2018 в 05:56
0

Now I'm interested in formulating, for example the batting average is calculated Hit between turns, the formula according to the tables would be H / T

Where in the query should I put it? I get lost because it is adding the 2 tables and not one.

select sum(T) T, sum(CA) CA, sum(H) Hits, sum(2H) 2H, sum(3H) 3H, sum(HR) HR, sum(BB) BB, sum(SF) SF from ( select T, CA, H, 2H, 3H, HR, BB, SF from stats where id=10 union all select T, CA, H, 2H, 3H, HR, BB, SF from stats2017 where id=6 ) x
    
answered by 06.01.2018 в 19:00