开发者

How to copy a field from a table paste into another table with same properties

I am searching this for hours.

I just want to copy some fields from one table into an another table. In the destination table the fields i want to copy doesn't exsist.

It's like, copy and paste.

Ex. I have to tables

Table A Like

`id` int(11) NOT NULL AUTO_INCREMEN开发者_运维技巧T,
`name` varchar(255) DEFAULT NULL,
`active` int(11) DEFAULT '0'

Table B like

`date` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`alias` varchar(255) DEFAULT NULL,
`voters` int(11) DEFAULT NULL,
`level` int(11) NOT NULL DEFAULT '0',
`content` text

Ex. I want to copy 'content , level, voters' from Table B to Table A with the same structure and data.

I have in my mind like this, But HOW?

Alter Table A Add content (same SCHEMA as TABLE B.content)
Alter Table A Add level (same SCHEMA as TABLE B.level)
Alter Table A Add level (voters SCHEMA as TABLE B.voters)


INSERT INTO A (col1  , col2 , col3) 
SELECT colB1, colB2,colB3 
FROM B 

they must be that same datatype and size

UPDATE if table A does not contain data you can create it like this

CREATE TABLE A LIKE B;

Use LIKE to create an empty table based on the definition of another table, including any column attributes and indexes defined in the original table:

then make the insert statement above

if you want to keep the data in table A and add some columns from table B you can merge these two table inside new Table X then you delete table A and rename X

CREATE TABLE X 
SELECT * from emp;

Note: if table A contains many record this will take too much time and is not a good solution

but your select statement will contains join , if there is a relationship between A and B tell me to write a more specific solution

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜