Possible Duplicate:
How can I merge two MySql tables?
我想把结构相同的多个表合并成一个大表。这些表的名称相似,所以我想使用 LIKE 语句。谁能告诉我该怎么做?
表格非常简单,每个表格都有一个ID列和一些其他列,但是表格的数量很大,所有表格的名称都像'TX-xxx'
,其中'TX'
表示得克萨斯州,'xxx'
是得克萨斯州的县;你知道得克萨斯州有 200 多个县。 (事实上,我必须对所有状态都这样做。)所以我想使用语句 "LIKE 'TX-___'"
。
谢谢!
您必须提供更多信息,以便我们确切知道您想要什么,但您可以创建一个 View
CREATE VIEW myViewName AS
select *
from table1
union all
select * from
table2
这样它会显示所有表中的信息(并且可以限制在选择中不显示所有内容)并且当表 1、表 2 等发生更改时, View 将反射(reflect)这一点。您可以随时更改它并像获取表格一样从中获取:
select * from myViewName
现在为了从特定表中抓取,我不确定如何在 mysql 中执行此操作,尽管我已经在 tsql 中完成了。 This previous question会帮助你所以你可能有这样的东西:
-- Create temporary table of varchar(200) to store the name of the tables. Depending on how you want to go through the array maybe an id number (int).
insert into tempTableName (name)
SELECT table_name FROM information_schema.tables WHERE table_schema = 'database_name' and table_name like 'TX_%';
declare @sqlQuery varchar(max)
--Then you will want to loop through the array and build up an sql statement
-- For each loop through:
if len(@sqlQuery) = 0 begin -- first time through
set @sqlQuery = 'select col1, col2, col3 from ' + currentTableName
end else begin -- second+ time through
set @sqlQuery = 'union all select col1, col2, col3 from ' + currentTableName
end
-- after the loop add the create view. Could double check it worked by checking length = 0 again
set @sqlQuery = 'CREATE VIEW myViewName AS ' + @sqlQuery
Once the query string is built up you will execute it with
PREPARE stmt FROM @sqlQuery;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;