首页 > 数据库 >php+sql 从一张表中查询出来的字段值添加到另一张的表中以及批量循环添加数据

php+sql 从一张表中查询出来的字段值添加到另一张的表中以及批量循环添加数据

时间:2022-08-19 16:44:57浏览次数:65  
标签:一张 code name sp 添加 location 表中 query row

$message=$result['message'];
// $ware_house_name = array_column($message,'WarehouseCode');
// $ware_house_name=array_unique($ware_house_name);
// $new_array=array_filter($ware_house_name);
// //009,001,005,007,003,010,002,008
// $string=implode(',',$new_array);
// $query = $DB->prepare("
//     SELECT 
//           location_id,
//           location_code,
//           location_name
//     FROM asset_location
//     WHERE location_code 
//     IN ($string)
// ");
// //$string='003';
// //$query->bindvalue(':params',$string);

// if (!$query->execute()) {
//  Flight::error(new RuntimeException(errorInfo($query)));
// } elseif ($query->rowcount()== 0) {
//  Flight::notFound();
// }

// $items = [];
// // 请求到了库房的数据
// while ($row = $query->fetch(PDO::FETCH_OBJ)) {
//     $items[] = $row;
// }
// $str='';
// foreach($items as $val){
    
//     $str+= $val->location_code;
//     $new_str=$str;
//}

$query = $DB->prepare("    
    INSERT INTO sp_list(
        sp_code,
        sp_name,
        sp_current_quantity,
        sp_specification,
        sp_unit,
        sp_location_id
    )
    SELECT
        :sp_code,
        :sp_name,
        :sp_current_quantity,
        :sp_specification,  
        :sp_unit,   
        t.location_id
    FROM
        asset_location t
    WHERE
        t.location_code = :code ;
");

foreach ($message as $row){

    $sp_code=$row->InventoryCode;
    $sp_name=$row->InventoryName;
    $sp_current_quantity=$row->ExistingQuantity;
    $sp_specification=$row->Specification;
    $sp_unit=$row->UnitName;
    $code=$row->WarehouseCode;

    $query->bindvalue(":sp_code",$sp_code );
    $query->bindvalue(":sp_name",$sp_name );
    $query->bindvalue(":sp_current_quantity",$sp_current_quantity );
    $query->bindvalue(":sp_specification",$sp_specification );
    $query->bindvalue(":sp_unit",$sp_unit );
    $query->bindvalue(":code",$code );
    $query->execute();
}

if (!$query->execute()) {
    Flight::error(new Exception(errorInfo($query)));
}

$new_id = $DB->lastInsertId();

 

标签:一张,code,name,sp,添加,location,表中,query,row
From: https://www.cnblogs.com/xiaoyantongxue/p/16602526.html

相关文章