mysql,使用select子查询插入

mysql,使用select子查询插入,mysql,Mysql,我有很多产品在产品类别表中,其类别id为5。 我添加了一个新的category\u id19。 如何将类别5中的所有产品也添加到类别19中 我正在尝试这样做: insert into product_categories( category_id, product_id ) select 19, (select product_id from product_categories where category_id = 5) INSERT INTO product_cat

我有很多产品在
产品类别
表中,其
类别id
为5。 我添加了一个新的
category\u id
19。 如何将类别5中的所有产品也添加到类别19中

我正在尝试这样做:

insert into product_categories( category_id, product_id )
select 19,
   (select product_id
    from product_categories
    where category_id = 5)
INSERT INTO product_categories( category_id, product_id )
SELECT 19, product_id FROM product_categories WHERE category_id =5
INSERT INTO table_a (field, field, timestamp) 
 VALUES (
     (SELECT id FROM tablex WHERE id = :xid LIMIT 1),
     (SELECT id FROM tabley WHERE id = :yid LIMIT 1),
     NOW()
 )
但是我得到了一个
子查询,它返回了超过1行的错误。

试试这个:

INSERT INTO product_categories (category_id, product_id)
  SELECT 19 AS category_id, product_id 
  FROM product_categories 
  WHERE category_id =5;

试着这样做:

insert into product_categories( category_id, product_id )
select 19,
   (select product_id
    from product_categories
    where category_id = 5)
INSERT INTO product_categories( category_id, product_id )
SELECT 19, product_id FROM product_categories WHERE category_id =5
INSERT INTO table_a (field, field, timestamp) 
 VALUES (
     (SELECT id FROM tablex WHERE id = :xid LIMIT 1),
     (SELECT id FROM tabley WHERE id = :yid LIMIT 1),
     NOW()
 )
有限度地尝试

INSERT INTO product_categories( category_id, product_id ) SELECT 19 , (

SELECT product_id FROM product_categories WHERE category_id =5 LIMIT 1)

您可以这样做:

insert into product_categories( category_id, product_id )
select 19,
   (select product_id
    from product_categories
    where category_id = 5)
INSERT INTO product_categories( category_id, product_id )
SELECT 19, product_id FROM product_categories WHERE category_id =5
INSERT INTO table_a (field, field, timestamp) 
 VALUES (
     (SELECT id FROM tablex WHERE id = :xid LIMIT 1),
     (SELECT id FROM tabley WHERE id = :yid LIMIT 1),
     NOW()
 )

一点也不需要,只要记住接受你所有问题的正确答案,这样,如果其他人有相同的问题,他们会更容易找到解决方案。你不应该真的需要限制,但如果你没有使用唯一的行id,那么就方便了