How can I convert one database row to two

I have a table with column id, itemID and locationId.

Now I split one locationId into several, so I want:

Refresh all records where locationId is 10, and have two lines where locationId is set to 11 and 12 (ItemId will be the same for both lines)

So, to get started, I:

Id ItemID LocationID
1 1 10

and I want to finish with

Id ItemID LocationID
1 1 11
1 1 12

is there any convenient way to do this given its updating and insertion right away

+3
source share
1 answer

, , , , :

begin transaction;
insert into TBL (id,itemid,locationid)
    select id, itemid, 11
    from TBL
    where locationid = 10;
update TBL
    set locationid = 12
    where locationid = 10;
commit transaction;

SQL , , , , : -)

, , ( , ), , , , , ).

+2

Source: https://habr.com/ru/post/1755892/


All Articles