将包含case语句的MySql查询转换为codeigniter活动记录查询

时间:2014-09-09 08:01:21

标签: php mysql sql codeigniter activerecord

我有以下查询,我想让Codeigniter Active Record更友好,但我不确定如何将case语句添加到查询的设置部分。

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

我已经尝试了这个,但加入似乎并不是......加入。

$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');

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

试试这个

$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
');