从子查询插入

时间:2013-03-06 10:28:28

标签: mysql magento

我正在尝试根据子查询的结果插入,但我一直得到同样的错误:

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
    )
))

如何为插入选择多个列?

3 个答案:

答案 0 :(得分: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列表中添加静态值

答案 1 :(得分: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
    )

答案 2 :(得分: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
   )