如何使用WHERE子句一次将多个值插入到postgres表中

时间:2017-11-22 19:23:43

标签: postgresql

我有一张表,我试图一次更新多个值。这是代码:

INSERT INTO movements 
        ( 
                    movements_datetime, 
                    movements_user_id, 
                    movements_product_id, 
                    movements_reason_id, 
                    movements_netto_price, 
                    movements_brutto_price, 
                    movements_quantity, 
                    movements_f 
        ) 
        VALUES 
        ( 
                    '" . date('y-m-dH:I:s') . "', 
                    '" . $id . "', 
                    unnest($product_id), 
                    15, 
                    ( 
                           SELECT product_purchase_price 
                           FROM   products 
                           WHERE  product_id=unnest($product_id)), 
                    0, 
                    0, 
                    'false' 
        )

错误:WHERE的参数不能返回设置LINE 1:...从产品WHERE product_id中选择product_purchase_price ... ^

PHP代码

for ($i=0; $i < count($_POST['actual_stock']); $i++) {
    if($_POST['actual_stock'][$i]!=''){
        $actual_stock[] = $_POST['actual_stock'][$i];
        $product_id[] = $_POST['product_id'][$i];
    }

}


$actual_stock= 'array['. implode(',', $actual_stock). ']';  
$product_id = 'array['. implode(',', $product_id). ']'; 

1 个答案:

答案 0 :(得分:0)

您不能以这种方式使用x <- structure(list(variable = structure(c(10L, 6L, 3L, 4L, 2L, 8L, 9L, 5L, 1L, 7L), .Label = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"), class = c("ordered", "factor")), value = c(0.990683229813665, 0.975155279503106, 0.928571428571429, 0.807453416149068, 0.717391304347826, 0.388198757763975, 0.357142857142857, 0.201863354037267, 0.173913043478261, 0.0496894409937888)), .Names = c("variable", "value"), row.names = c(10L, 6L, 3L, 4L, 2L, 8L, 9L, 5L, 1L, 7L), class = "data.frame") 。如果您想从VALUES检索对(product_id, price),则必须使用products子句中的any(array)查询该表:

WHERE

insert语句应如下所示:

select product_id, price
from products
where product_id = any(array[10, 20, 30]);