简体   繁体   中英

copying mysql rows within single table

I have a mySQL database and am looking to update some rows with new values, but leave the originals intact.

For example: Within the site_files table, there is a 'site_id' column. I have a bunch of rows where site_id' = 981 but I want to change that to site_id' = 2016 (and leave the original rows in 981 .

I hope that makes sense. I know it would be a simple matter to use the UPDATE statement, but I want to leave the rows that read 981 as well.

Thanks in advance for any ideas.

INSERT INTO site_files (site_id, colA, colB, ...)
SELECT '2016', colA, colB, ...
FROM site_files WHERE site_id = '981'

this will get what you want, this is insert, not update

I think what you are trying to do is, add another site_id, with value 2016, but rest of the column going to match 981, if thats the case, and 981 going to remain means you dont want to delete it

you can follow these steps;

run the select query where site_id=981, get the value for all the column,

now run the insert query give the value for site_id=2016, give other column the value which you got from select query,

otherwise post your code some one will help you,

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