1

我正在尝试根据子查询的结果进行插入,但我不断收到相同的错误:

Operand should contain 1 column(s)

这是查询:

INSERT INTO customer_entity_int

(attribute_id, entity_type_id, entity_id, `value`)

VALUES (14, (
    SELECT entity_type_id, entity_id, `value`
    FROM customer_entity_int
    WHERE attribute_id = 13
    AND entity_id NOT
    IN (
        SELECT entity_id
        FROM customer_entity_int
        WHERE attribute_id = 14
    )
))

如何为我的插入选择多于一列?

4

3 回答 3

3

您将要使用INSERT INTO...SELECT FROM而不是INSERT INTO..VALUES

INSERT INTO customer_entity_int (attribute_id, entity_type_id, entity_id, `value`)
SELECT 14, entity_type_id, entity_id, `value`
FROM customer_entity_int
WHERE attribute_id = 13
  AND entity_id NOT IN (SELECT entity_id
                        FROM customer_entity_int
                        WHERE attribute_id = 14)

您可以在SELECT列表中包含一个静态值attribute_id

于 2013-03-06T10:30:44.790 回答
2

尝试dis:

INSERT INTO customer_entity_int

(attribute_id, entity_type_id, entity_id, `value`)

    SELECT 14, entity_type_id, entity_id, `value`
    FROM customer_entity_int
    WHERE attribute_id = 13
    AND entity_id NOT
    IN (
        SELECT entity_id
        FROM customer_entity_int
        WHERE attribute_id = 14
    )
于 2013-03-06T10:30:04.510 回答
0

请尝试以下代码......

INSERT INTO customer_entity_int
(attribute_id, entity_type_id, entity_id, `value`)
SELECT 14, entity_type_id, entity_id, `value`
FROM customer_entity_int
WHERE attribute_id = 13
AND entity_id NOT
IN (
    SELECT entity_id
    FROM customer_entity_int
    WHERE attribute_id = 14
   )
于 2013-03-06T10:36:00.040 回答