【问题标题】:Codeigniter: Update multiple Rows in Codeigniter in one statementCodeigniter:在一个语句中更新 Codeigniter 中的多行
【发布时间】:2017-07-28 14:58:32
【问题描述】:

我有字典

array(
  'ID1' => 1 ,
  'ID2' => 2,
  'Status' => 'Done'
)

虽然我的表中只有 2 列 ID 和状态。我想只使用一次 ($this->db->update) 更新两行 ID 为 1 和 2 的行。是否有可能或者我必须进行自定义查询。 我想这样做 ($this->db->update(其中 ID1 == 1 && ID2 == 2))。

【问题讨论】:

    标签: sql codeigniter


    【解决方案1】:

    任一

    // Get your status 
    $item = array( 'Status' => $response['Status'] );
    
    // unset status
    unset($response['Status']);
    
    // I assume except status rest all values are ids so
    // array_values gives id
    $this->db->where_in('id', array_values($response));
    
    // Update table
    $this->db->update('table', $item );
    

    你必须修改你的数组

        $data = array(
           array(
             'id' => 1 ,
             'Status' => 'Done'
           ),
           array(
             'id' => 2 ,
             'Status' => 'Done'
           )
        );
    

    然后

        $this->db->update_batch('table_name', $data, 'id');
    

    这样你可以修改

    (评论:亲爱的,我不能这样做,因为我收到了一些服务的回复。我无法编辑这个回复。)

    [akshay@localhost tmp]$ cat test.php
    <?php
    $response = array(
      'ID1' => 1 ,
      'ID2' => 2,
      'Status' => 'Done'
    );
    
    $data = array(); 
    $item = array( 'Status' => $response['Status'] );
    unset($response['Status']);
    
    foreach($response as $new){
            $item['id'] = $new;
            $data[] = $item; 
    }
    print_r($data);
    
    // Here you update 
    // $this->db->update_batch('table_name', $data, 'id');
    ?>
    

    输出

    [akshay@localhost tmp]$ php test.php
    Array
    (
        [0] => Array
            (
                [Status] => Done
                [id] => 1
            )
    
        [1] => Array
            (
                [Status] => Done
                [id] => 2
            )
    
    )
    

    【讨论】:

    • 亲爱的,我不能这样做,因为我收到了一些服务的回复。而且我无法编辑此回复。
    • @user6159419 请看我的编辑,你可以从这里开始
    【解决方案2】:
    //this will update the values where ids equal any value in the second were_in parameter
    
    //data is array of values to update 
    $data = [
       'Status'=>'some status',
    ];
    
    //array of ids to update
    $ids = [1,2];
    
    $this->db->where_in('id', $ids)->update('table_name', $data);
    

    【讨论】:

      猜你喜欢
      • 2012-08-27
      • 2017-10-08
      • 1970-01-01
      • 1970-01-01
      • 2016-10-07
      • 1970-01-01
      • 1970-01-01
      • 2020-04-24
      • 1970-01-01
      相关资源
      最近更新 更多