Mairaj Khan
Mairaj Khan

Reputation: 359

copy data of particular id from one table and paste at particular id of another table

I have a problem: I have to tables examp1 and examp2. I want to copy data of examp1 whose id=138 and paste that into another table examp2 at id=5. I tried this

insert into examp1 (rates,publishPrice,name) 
 select rates,publishPrice,name from examp2 where id=138;

and

insert into examp1 (rates,publishPrice,name) where id=5 
 select rates,publishPrice,name from examp2 where id=138;`

but I am getting problem in 2nd query. While in 1st query the data of id=138 is copied but not at particular id.

Upvotes: 0

Views: 191

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269803

If the id with 5 does not exist in examp1, perhaps you want:

insert into examp2(id, rates, publishPrice, name)
    select 5, rates, publishPrice, name
    from examp1
    where id = 138;

If the ids both exist, then you want an update:

update examp2 cross join
       (select e1.*
        from examp1 e1
        where id = 138
       ) e1
       on examp2.id = 5 
     set e2.rates = e1.rates,
         e2.publishPrice = e1.publishPrice,
         e2.name = e1.name;

Or something like this, based on what your question really is.

Upvotes: 2

Related Questions