2013-03-06 86 views
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 
    ) 
)) 

哪有我为我的插入选择多个列?

回答

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

+0

完美,这工作很好,我会尽快接受它 – BenOfTheNorth 2013-03-06 10:32:06

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