简体   繁体   English

将Mysql列从一个表添加到另一个表

[英]Adding Mysql columns from one table to another

I have two MySQL tables: one called Audit and the other Audit2. 我有两个MySQL表:一个称为Audit表,另一个称为Audit2表。 I need to copy the column names from Audit2 to Audit. 我需要将列名称从Audit2复制到Audit。 For example table Audit has columns 1, 2 and 3 and Audit2 has columns 4, 5 and 6 and I need Audit to have columns 1, 2, 3, 4, 5 and 6. 例如,表Audit具有第1、2和3列,而Audit2具有第4、5和6列,而我需要Audit具有第1、2、3、4、5和6列。

I tried the following to no success: 我尝试以下操作未成功:

ALTER TABLE  `Audit` ADD  (select `COLUMN_NAME` from information_schema.columns
where table_schema = 'BMS' and `TABLE_NAME` = 'Audit2') (select `DATA_TYPE` from information_schema.columns
where table_schema = 'BMS' and `TABLE_NAME` = 'Audit2') NOT NULL

You can do it by 你可以做到

create table new_audit as
select t1.*,t2.* from audit as t1 inner join audit_2 as t2 on 1=0;

The table new_audit will have all the columns 表new_audit将具有所有列

Have you tried: 你有没有尝试过:

SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='your_table_name';  

It'll return all the column names in a single column from the table you want. 它将在所需表的单个列中返回所有列名。

If you have a PHP environment set up, you can download this tool: http://www.mysqldiff.org/downloads.php It's brilliant - will compare two mysql database schema's and generate the mysql code that will sync the structures together, sounds just like what you need and handy for the future too. 如果您设置了PHP环境,则可以下载此工具: http : //www.mysqldiff.org/downloads.php很棒-将比较两个mysql数据库模式,并生成将结构同步在一起的mysql代码,听起来就像您需要的东西一样,也方便将来使用。

If not. 如果不。 I'd do just what you're doing, but probably just simplify it a bit for the sake of a one off instance and 3 columns. 我会做您正在做的事情,但可能只是为了简化实例和3列而简化了一下。

Something like ALTER TABLE Audit ADD yourcolumnname VARCHAR(100); ALTER TABLE Audit ADD yourcolumnname VARCHAR(100);

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

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