开发者

Problems copying data from one table to another via UPDATE and INSERT INTO

开发者 https://www.devze.com 2023-03-15 19:35 出处:网络
I have two tables and need to move data from one to the other. I just want to move specific values from the first column, i.e. if they = ni (varchar) to a new column in the second.

I have two tables and need to move data from one to the other. I just want to move specific values from the first column, i.e. if they = ni (varchar) to a new column in the second.

So I'd like to select the nr data below from table lime_survey_56999

`id` `56999X159X3400`
1    2
2    6                  
3    nr
4    mi

And move it into the new table lime_survey_56999_cube so it reads

`id`  `External-Unique-Factors4NR`
1     NULL
2     NULL                  
3     nr
4     NULL

I can't use INSERT INTO because there is data already in lime_survey_56999_cube and I need this to match the original row in the first table.

Below are my attempts and their outcomes/error messages

UPDATE `lime_survey_56999_cube` set `lime_survey_56999_cube`.`External-Unique-Factors4NR` = (select `56999X159X3400` from `lime_survey_56999` where `56999X159X3400` = 'nr')

Error #1242 - Subquery returns more than 1 row

UPDATE `lime_survey_56999_cube` 
SET `External-Unique-Factors4NR` = 'nr'
WHERE `56999X159X3400` in (select `56999X159X3400` from `lime_survey_56999` where `56999X159X3400` = 'nr');

Error #1054 - Unknown column '56999X159X3400' in 'IN/ALL/ANY subquery'

UPDATE `lime_survey_56999_cube` 
INNER JOIN `lime_survey_56999` 
SET `lime_survey_56999_cube`.`External-Unique-Factors4NR` = `lime_survey_56999`.`56999X159X3400` 
WHERE `lime_survey_56999`.`56999X159X3400` = 'nr'

This only puts nr into all the columns of External-Unique-Factors4NR, rather than where it corresponds to the entry in the first table.

Any help wou开发者_StackOverflow社区ld be very much appreciated! I've been tearing my hair out over this!


you are missing the ON clause for your join

UPDATE `lime_survey_56999_cube`
INNER JOIN `lime_survey_56999` 
ON `lime_survey_56999_cube`.???? = `lime_survey_56999`.????
SET `lime_survey_56999_cube`.`External-Unique-Factors4NR` = `lime_survey_56999`.`56999X159X3400` 
WHERE `lime_survey_56999`.`56999X159X3400` = 'nr'

replace ???? with the corresponding column names

0

精彩评论

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