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