简体   繁体   English

如何在单个查询中针对单个category_id添加多个product_id

[英]How to add multiple product_ids against single category_id in single query

I have a table -> table_product_categories - "product_id", "category_id" 我有一个表-> table_product_categories-“ product_id”,“ category_id”

I want to add multiple product ids (that are in range) against each category. 我想针对每个类别添加多个产品ID(在范围内)。 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

You can't generate sequence numbers in MySQL. 您无法在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

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) . 要添加范围,您应该迭代到该范围并制作一个字符串,如(11, 1), (12,1), (13,1) Then execute a query. 然后执行查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM