Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy rows and change 1 value? [duplicate]

Tags:

sql

How would I copy entire rows within a table and change one value?

 insert into user (
     user_id,account_id,user_type_cd,name,e_mail_addr,
     login_failure_cnt,admin_user,primary_user
 )
 select * from pnet_user where account_id='1'

But now I want to change 1 to 2 on the inserted entries. But now I want to change 1 to 3 on the inserted entries. But now I want to change 1 to .... on the inserted entries. But now I want to change 1 to 1000 on the inserted entries.

It will copy and write down 1000 times (only changing id ).

like image 919
Ch L Avatar asked May 21 '13 12:05

Ch L


1 Answers

I'm not completely sure I understand what you're asking. If you want to copy the records where Account_ID = 1 into new rows and change Account_ID to 2 (or whatever number), this should work for you:

insert into user (user_id,account_id,user_type_cd,name,e_mail_addr,
     login_failure_cnt,admin_user,primary_user)
select user_id,2,user_type_cd,name,e_mail_addr,
     login_failure_cnt,admin_user,primary_user 
from pnet_user where account_id='1'

Basically, replace Account_ID with the value 2. If Account_ID is a varchar, use single quotes around it instead.

like image 85
sgeddes Avatar answered Oct 31 '22 18:10

sgeddes