I have some History Tables with Structure of "TableNameYYMM" How can I UNION all History Tables which Starts with "TableName"?
I need a search over all History Tables.
I tryed it with PREPARE Execute and DEALLOCATE. But i get everytime a SQL Error.
SET group_concat_max_len = 2048;
SET @startQuery = (SELECT CONCAT('SELECT col1, col2, col3 FROM ',TABLE_SCHEMA,'.', TABLE_NAME)
FROM information_schema.tables
WHERE
ENGINE = 'MyISAM'
AND TABLE_NAME like 'hist%'
ORDER BY TABLE_NAME
LIMIT 0,1);
SET @subquery = (SELECT GROUP_CONCAT(@startquery, 'UNION ALL SELECT col1, col2, col3 FROM ',TABLE_SCHEMA,'.', TABLE_NAME)
FROM information_schema.tables
WHERE
ENGINE = 'MyISAM'
AND TABLE_NAME like 'hist%'
ORDER BY TABLE_NAME
LIMIT 1,1000);
PREPARE stmt1 FROM '? AS combinedTable';
EXECUTE stmt1 USING @subquery;
DEALLOCATE PREPARE stmt1;
On Part 1 (@startquery) i try to get the first part of the Query "select xxx from table1"
On Part 2 (@subquery) I tried to get all unions (from table2-max 1000)
select xxx from table1
UNION ALL select xxx from table2
UNION ALL select xxx from table3
...
I hope someone have an idea about this problem.