简体   繁体   中英

Convert MySql query containing case statement to codeigniter active record query

I have the following query which I want to make more Codeigniter Active Record friendly but I'm not sure how to go about adding case statements to the set part of the query.

UPDATE attendance_time_index ati
JOIN users s ON ati.time_index_user_id = s.user_id
JOIN attendance_code_groups acg ON s.user_attendance_code_group = acg.group_id
SET ati.time_index_default = CASE WHEN ati.time_index_value = 'PM' THEN acg.group_pm_default ELSE acg.group_am_default END
WHERE s.user_id = 123

I've tried this, but the joins don't seem to be... joining.

$this->db->join('users s','ati.time_index_user_id = s.user_id');
$this->db->join('attendance_code_groups acg', 's.user_attendance_code_group = acg.group_id');
$this->db->set('ati.time_index_default', 'CASE WHEN ati.time_index_value = 'PM' THEN acg.group_pm_default ELSE acg.group_am_default END', FALSE);
$this->db->where('s.user_id', 123);
$this->db->update('attendance_time_index ati');

Any ideas?

Try this

$this->db->set('ati.time_index_default', "CASE WHEN ati.time_index_value = 'PM' THEN acg.group_pm_default ELSE acg.group_am_default END", FALSE);
$this->db->where('s.user_id', 123);
$this->db->update('attendance_time_index ati
join users s on ati.time_index_user_id = s.user_id
join attendance_code_groups acg  on s.user_attendance_code_group = acg.group_id
');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM