Jack Morgan Jack Morgan - 1 month ago 18
MySQL Question

Sum tables MySQL

I have 4 tables in my database. 2012,2013,2014,2015.

Each one is laid out as so:

enter image description here

I would like to sum each StartStation for each year as so:

StartStation 2012 2013 2014 2015
10th & E St NW X Y Z ...
10th & Monroe St NE X Y Z ...
......


Thank you!!

Answer

Try this:

select
  t1.startstation,
  t1.totalcount `2012`,
  t2.totalcount `2013`,
  t3.totalcount `2014`,
  t4.totalcount `2015`
from test.`2012` t1
join test.`2013` t2 on t2.startstation = t1.startstation
join test.`2014` t3 on t3.startstation = t1.startstation
join test.`2015` t4 on t4.startstation = t1.startstation
Comments