2013-12-20 89 views
-2

我有這種先進的UPDATE查詢:轉換高級更新查詢笨的Active Record

UPDATE `images` SET `cover` = 
CASE WHEN `image_id` = {$is_cover} THEN 1 
ELSE 0 END WHERE `album_id` = {$album_id} 

我怎樣才能將其轉換爲CodeIgniter的Active Record的請?

回答

1

請使用

$this->db 
    ->set('cover', 'CASE WHEN `image_id` = $is_cover THEN 1 ELSE 0 END', FALSE) 
    ->where('album_id', $album_id) 
    ->update('images'); 
0

,您可以嘗試這樣

$data = array('cover' => 'CASE WHEN `image_id` = {$is_cover} THEN 1 ELSE 0 END'); 
$where = array('album_id' => $album_id); 
$this->db 
    ->where($where) 
    ->update('images',$data); 
0

您可以直接用它查詢笨:

$this->db->query("UPDATE images SET cover = CASE WHEN image_id = {$is_cover} THEN 1 ELSE 0 END WHERE album_id = {$album_id}");