开发者

How to copy 1 column from one db to another db and set constants for another column in the 1st db during the INSRT INTO statement?

开发者 https://www.devze.com 2023-03-13 01:01 出处:网络
I开发者_如何学运维 have database1 with table table1 and 3 columns: column1a, column1b, column1c

I开发者_如何学运维 have database1 with table table1 and 3 columns: column1a, column1b, column1c I have database2 with table table2 and 1 columns: column2a

I would like to copy the values from the database2 (from column2a) to database1 column1a and the others values of column1b and column1c set to a certain constant value e.g.

INSERT INTO database1.table1 (column1a,'column1b' = 'apple','column1c'= 'orange') SELECT `column2a` FROM database2.table2;

However, in the fisrt part if the statemnt are 3 values and in the second is only 1, so I suppose there is a problem and this statement is bad :(.

Any advice how to rewrite my mysql statement?


Here's the solution:

INSERT INTO database1.table1 (column1a, column1b, column1c)
SELECT column2a, 'apple', 'orange' FROM database2.table2


Try this:

INSERT INTO `database1`.`table1` (`column1a`, `column1b`, `column1c`)
SELECT `column2a`, 'apple', 'orange' FROM `database2`,`table2`;


If columns are different i always have success with the below:

USE `old_database`;
INSERT INTO `new_database`.`new_table`(`column1`,`column2`,`column3`)
SELECT `old_table`.`column2`, `old_table`.`column7`, `old_table`.`column5` 
FROM `old_table`
0

精彩评论

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