I have the following query in mysql 5.1.41:
select distinct table_schema from information_schema.tables where table_schema like '%dog%';
I want to take the output of that command:
+-------------------+ | table_schema | +-------------------+ | dog_a | | dog_b | +-------------------+
and then use the database names as input to a query like the following:
select count(*) from (select * from dog_a.log where insane = 1 UNION ALL select * from dog_b.log where insane = 1) as total_count;
such that the algorithm is essentially:
For each database in databases, count the number of insane dogs and sum the total across all databases. However, I don't know how to wrap up the two queries to get the database names from the first query as iterable input into the second query within mysql.
I need to be able to do this entirely within the database.
Any ideas?
Thanks!