Is it possible to combine these two queries into one? I am using MySQL.
SELECT SUM(Col1), SUM(Col2) FROM TableA WHERE Col3 = 'sam';
SELECT SUM(Col5) FROM TableB WHERE Col3 = 'sam';
Yes (as far as i know)..
You can do something like: SELECT SUM(TableA.Col1), SUM(TableA.Col2), SUM(TableB.Col5) FROM TableA, TableB WHERE Col3='sam';
--Jamie
You can try using a JOIN:
SELECT SUM(Col1), SUM(Col2), SUM(Col5) FROM TableA INNER JOIN TableB ON TableA.id = TableB.id WHERE Col3 = 'sam';
TableA.id = TableB.id is the link between the two tables. Hope this helps.
While each of my queries works fine when run separately both recommended combined queries output Col1, Col2 & Col5 sum values that are double what they should be. Any ideas?
Well cant you just halve the results?
Im not quite sure why it would be double though.. quite weird..
All three columns returned double but on other queries that return different results some of the results were increased by larger multiples (triple, quadruple and more) than what they should have been) with no detectable pattern so deviding the results by 2 will not always work. Anyone have any ideas?