$this->db->update_batch();
生成一條update命令是以你提供的數據為基礎的,並執行查詢。你可以傳遞一個數組或對象的參數給update_batch()函數。下面是一個使用一個數組作為參數的示例:Generates an update string based on the data you supply, and runs the query. You can either pass an array or an object to the function. Here is an example using an array:
1 $data = array( 2 array( 3 'title' => 'My title' , 4 'name' => 'My Name 2' , 5 'date' => 'My date 2' 6 ), 7 array( 8 'title' => 'Another title' , 9 'name' => 'Another Name 2' , 10 'date' => 'Another date 2' 11 ) 12 ); 13 14 $this->db->update_batch('mytable', $data, 'title'); 15 16 // Produces: 17 // UPDATE `mytable` SET `name` = CASE 18 // WHEN `title` = 'My title' THEN 'My Name 2' 19 // WHEN `title` = 'Another title' THEN 'Another Name 2' 20 // ELSE `name` END, 21 // `date` = CASE 22 // WHEN `title` = 'My title' THEN 'My date 2' 23 // WHEN `title` = 'Another title' THEN 'Another date 2' 24 // ELSE `date` END 25 // WHERE `title` IN ('My title','Another title')
參數1:表名 參數2:如上所示的二維數組 參數3:鍵名.
提示: 所有的值都會自動進行安全性過濾.
即:
UPDATE `mytable`
SET `name` = CASE
WHEN `title` = 'My title' THEN
'My Name 2'
WHEN `title` = 'Another title' THEN
'Another Name 2'
ELSE
`name`
END,
`date` = CASE
WHEN `title` = 'My title' THEN
'My date 2'
WHEN `title` = 'Another title' THEN
'Another date 2'
ELSE
`date`
END
WHERE
`title` IN ('My title', 'Another title')
-----------------------------------------------------------
比如要批量更新狀態未讀為已讀:
$data = array(
array(
'id' => '1' ,
'status' => 'READ'
),
array(
'id' => '2' ,
'status' => 'READ'
)
);
$this->db->update_batch('mytable', $data, 'id');