MYSQL 如何联合多个 "Unknown" 表
MYSQL How to UNION Multiple "Unknown" Tables
我有一些结构为 "TableNameYYMM" 的历史表
如何 UNION 所有以 "TableName" 开头的历史表?
我需要搜索所有历史表。
我用 PREPARE Execute 和 DEALLOCATE 试过了。
但我每次都会收到 SQL 错误。
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;
在第 1 部分 (@startquery) 我尝试获取查询的第一部分
"select xxx from table1"
在第 2 部分 (@subquery) 我试图获取所有联合(来自 table2-max 1000)
select xxx from table1
UNION ALL select xxx from table2
UNION ALL select xxx from table3
...
我希望有人对这个问题有想法。
/*
create table history1701 (id int,col1 int,col2 int);
create table history1702 (id int,col1 int,col2 int);
create table history1703 (id int,col1 int,col2 int);
create table history1704 (id int,col1 int,col2 int);
insert into history1701 values (1,1,1);
insert into history1702 values (2,2,2);
insert into history1703 values (3,3,3);
insert into history1704 values (4,4,4);
*/
SET @startQuery = (SELECT CONCAT('SELECT col1, col2 FROM ',TABLE_SCHEMA,'.', TABLE_NAME)
FROM information_schema.tables
WHERE
# ENGINE = 'MyISAM' AND
TABLE_NAME like 'hist%17%'
ORDER BY TABLE_NAME
LIMIT 0,1);
SET @subquery = (
SELECT group_CONCAT(' UNION ALL SELECT col1, col2 FROM ',TABLE_SCHEMA,'.', TABLE_NAME order by table_name separator ' ' )
FROM information_schema.tables
WHERE
TABLE_NAME like 'hist%17%'
and table_name <> (
select table_name from information_schema.tables WHERE
# ENGINE = 'MyISAM' AND
TABLE_NAME like 'hist%17%'
ORDER BY TABLE_NAME
LIMIT 0,1
)
);
select @startquery;
set @subquery = concat(@startquery,@subquery);
PREPARE stmt1 FROM @subquery;
EXECUTE stmt1 ;
DEALLOCATE PREPARE stmt1;
在你的代码中,第一个限制 1,1000 @subquery returns 一个空值(不好)并在每个 union all 之后生成一个逗号(也不好)。我对此进行了修改以排除第一个历史记录 table 并将组连接分隔符更改为 space 并将顺序移动到 group_concat.
内
我有一些结构为 "TableNameYYMM" 的历史表 如何 UNION 所有以 "TableName" 开头的历史表?
我需要搜索所有历史表。
我用 PREPARE Execute 和 DEALLOCATE 试过了。 但我每次都会收到 SQL 错误。
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;
在第 1 部分 (@startquery) 我尝试获取查询的第一部分 "select xxx from table1"
在第 2 部分 (@subquery) 我试图获取所有联合(来自 table2-max 1000)
select xxx from table1
UNION ALL select xxx from table2
UNION ALL select xxx from table3
...
我希望有人对这个问题有想法。
/*
create table history1701 (id int,col1 int,col2 int);
create table history1702 (id int,col1 int,col2 int);
create table history1703 (id int,col1 int,col2 int);
create table history1704 (id int,col1 int,col2 int);
insert into history1701 values (1,1,1);
insert into history1702 values (2,2,2);
insert into history1703 values (3,3,3);
insert into history1704 values (4,4,4);
*/
SET @startQuery = (SELECT CONCAT('SELECT col1, col2 FROM ',TABLE_SCHEMA,'.', TABLE_NAME)
FROM information_schema.tables
WHERE
# ENGINE = 'MyISAM' AND
TABLE_NAME like 'hist%17%'
ORDER BY TABLE_NAME
LIMIT 0,1);
SET @subquery = (
SELECT group_CONCAT(' UNION ALL SELECT col1, col2 FROM ',TABLE_SCHEMA,'.', TABLE_NAME order by table_name separator ' ' )
FROM information_schema.tables
WHERE
TABLE_NAME like 'hist%17%'
and table_name <> (
select table_name from information_schema.tables WHERE
# ENGINE = 'MyISAM' AND
TABLE_NAME like 'hist%17%'
ORDER BY TABLE_NAME
LIMIT 0,1
)
);
select @startquery;
set @subquery = concat(@startquery,@subquery);
PREPARE stmt1 FROM @subquery;
EXECUTE stmt1 ;
DEALLOCATE PREPARE stmt1;
在你的代码中,第一个限制 1,1000 @subquery returns 一个空值(不好)并在每个 union all 之后生成一个逗号(也不好)。我对此进行了修改以排除第一个历史记录 table 并将组连接分隔符更改为 space 并将顺序移动到 group_concat.
内