MySQL cannot execute summary queries, but you can do this in two queries, using the result of the first query as SQL for the following query:
SELECT 'SELECT table_name' UNION SELECT CONCAT(', SUM(IF(import_date = "',import_date,'", num_recs,0)) AS "',DATE_FORMAT(import_date, "%m-%d"),'"') FROM event_log GROUP BY import_date UNION SELECT 'FROM event_log GROUP BY table_name'
Then run the output of this query to get the final results, for example. for your example you will get:
SELECT table_name , SUM(IF(import_date = "2010-06-20", num_recs,0)) AS "06-20" , SUM(IF(import_date = "2010-06-21", num_recs,0)) AS "06-21" , SUM(IF(import_date = "2010-06-22", num_recs,0)) AS "06-22" FROM event_log GROUP BY table_name
You can either write a stored procedure to concatenate, prepare and execute the results of the first query, or if it is all run from a shell script, you can commit the results of the first query, then return the results to mysql.
runrig
source share