natas
natas

Reputation: 456

Move mysql data from rows to a pivot table

I've a table products with:

ID | shop_id | ...

I wanto to convert id <-> shop_id relations to a pivot table and I need a query to avoid to make this job manually.

My pivot table product_shop have:

ID | product_id | shop_id

UPDATE WITH EXAMPLE

I've in product table this rows:

id | shop_id | name
1  | 4       | Tablet Samsung
3  | 10      | Iphone

I need to populate my brand new pivot table product_shop (now empty) with:

ID | product_id | shop_id
1  | 1          | 4
2  | 3          | 10

Thanks!

Upvotes: 0

Views: 35

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133380

Looking to your schema and sample
assuming you have the pivot table product_shop with

  id autoincrement,
  product_id,
  shop_id 

you could use a insert select

 insert into product_shop (  product_id,   shop_id )
 select id, shop_id 
 from product

Upvotes: 1

Related Questions