简体   繁体   English

将多个表转换为一个表mysql databae

[英]Converting many table into one table mysql databae

I have a mysql database with hundreds of tables. 我有一个带有数百个表的mysql数据库。 I want to convert this into a single table, where the table name will be added to the records, meaning that for each row / data in a particular table, I will add a new field, where in this new field the value will be the table name. 我想将其转换为单个表,在该表中将表名添加到记录中,这意味着对于特定表中的每一行/数据,我将添加一个新字段,在该新字段中,值将为表名。 Is there any way to do this automatically, rather than I have to create a new field on every table and insert the table name manually, this will take year. 有什么方法可以自动执行此操作,而不是我必须在每个表上创建一个新字段并手动插入表名,这将需要一年的时间。 The record that I have on this db is around 400000 records. 我在该数据库上的记录约为400000条记录。 Can mysqldump with a script do this? mysqldump可以用脚本执行此操作吗? or any other way ? 或其他方式?

My suggestion is to generate syntax of the form: 我的建议是生成以下形式的语法:

create table xxx as
    select t.*, 'table1' as TableName from table1 t union all
    select t.*, 'table2' as TableName from table2 t union all
    . . .

You can create the select statements using: 您可以使用以下方法创建选择语句:

select concat('select t.*, ''', Table_Name, ''' as TableName from ', Table_Name, ' union all '
from Information_Schema.Tables

You can then put the create table line in front and remove the final union all . 然后,您可以将create table行放在最前面,并删除最终的union all

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM