MySQL to Codeigniter Active Record删除重复记录

时间:2017-01-02 20:25:51

标签: php mysql codeigniter

目前在我的Codeigniter网站中,以下工作方法可以在与Google日历同步后删除重复项:

        $this->db->query("DELETE `e2`.* FROM `ea_appointments` AS `e1`, `ea_appointments` AS `e2` 
        WHERE `e1`.`id` > `e2`.`id` AND LEFT(`e1`.`id_google_calendar`,26) = LEFT(`e2`.`id_google_calendar`,26) 
        AND `e2`.`is_unavailable` = 0");

如何以Active Record格式表达?

1 个答案:

答案 0 :(得分:0)

$this->db->where('e1.id > e2.id');
$this->db->where('LEFT(e1.id_google_calendar,26)=','LEFT(e2.id_google_calendar,26)');
$this->db->where('e2.is_unavailable = 0');
$this->db->delete('ea_appointments e1','ea_appointments e2');

LEFT()SQL:获取表的第2个参数[如id或name ...],第二个参数是该字段左边的字符数,例如:

  

左(姓名,3)

假设名称等于 hello word 而不是LEFT返回 hel

  $this->db->delete('ea_appointments e1','ea_appointments e2');

在本节中,我们从主表名称创建别名,以便于使用。