简体   繁体   中英

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

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, ie 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 would 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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