1

I have a table -> table_product_categories - "product_id", "category_id"

I want to add multiple product ids (that are in range) against each category. How can i do that?

I am using:

insert into table_product_categories values (11, 1);
insert into table_product_categories values (12, 1);
insert into table_product_categories values (13, 1);

Is there any way where we can achieve the same in single query?

Devesh

Devesh Agrawal
  • 8,982
  • 16
  • 82
  • 131

2 Answers2

1

You can't generate sequence numbers in MySQL. You could use a temporary table with sequence numbers to achieve that

insert into table_product_categories 
select seq_num, 1
from your_temp_table
where seq_num between 11 and 30
order by seq_num asc
juergen d
  • 201,996
  • 37
  • 293
  • 362
  • I know this approach. but even here i need to mention (11, 1), (12, 1), (13, 1); But since product_id is in range, there must be a way where i can mention add product_id =11to30 in category_id = 1 – Devesh Agrawal Oct 14 '13 at 08:27
0

You can add multiple rows in single query.

insert into table_product_categories values (11, 1), (12,1), (13,1);

To add range you should iterate to that range and make a string like (11, 1), (12,1), (13,1). Then execute a query.

Parixit
  • 3,829
  • 3
  • 37
  • 61
  • Don't know it will be helpful or not! But you should look at here http://stackoverflow.com/q/186756/631652 – Parixit Oct 14 '13 at 08:31