Hi,
I have this query:
SELECT AVG(balance) FROM accountsABC
then I want to collect the same stats for other table (same schema, just different data). Eg: accountsABC, accountsDEF....
How could I do this with just one query? Thanks.
| SitePoint Sponsor |





Hi,
I have this query:
SELECT AVG(balance) FROM accountsABC
then I want to collect the same stats for other table (same schema, just different data). Eg: accountsABC, accountsDEF....
How could I do this with just one query? Thanks.





You would have to UNION them together:
Or use subqueries:Code:SELECT AVG( balance ) FROM t1 UNION SELECT AVG( balance ) FROM t2 etc.
Code:SELECT ( SELECT AVG( balance ) FROM t1 ) AS t1bal, ( SELECT AVG( balance ) FROM t2 ) AS t2bal, etc.
Matt - Sybase DBA / PHP fanatic
Sybase/MySQL/Oracle | I don't like MySQL
Download Sybase | DBForums.com - for all your RDBMS talk





It seems that until MySQL 4.0 it supports UNION, but my version is 3.23.36. And I'm not sure if MySQL support sub selects (it does, but in a different form I think). Tough luck! A script will do, but a query is certainly nicer
Thanks Matt
Bookmarks