How to copy one record become two record to another table?


I want to change table schema. From table_a to a new schema table_b.

table_a_id      BIGINT
origin_id          BIGINT
destination_id      BIGINT
point            INT

table_b_id      BIGINT
place_id              BIGINT
point            INT

I want to save every record in table_a become two record each table_b (second record point will be negative). For example:

table_a(table_a_id, origin_id, destination_id, point)
1, 10, 20, 100
2, 10, 30, 150
3, 20, 10, 200

table_b(table_b_id, place_id, point)
1, 10, 100
1, 20, -100
2, 10, 150
2, 30, -150
3, 20, 200
3, 10, -200

How to do that, by using storedprocedure? variable?

Thank you very much.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
you can do like this:
insert into table_b ( table_b_id, place_id, point)
select table_a_id, origin_id, point from table_A
union all
select table_a_id, destination_id, -point from table_A

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tikusbalapAuthor Commented:
Thanks. Works like a charm :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.