Currently in my Codeigniter site the following works to delete duplicates after sync with google calendar:
$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");
How can I express this in the Active Record format?
$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: get 2 parameter first field of table[like id or name...] and second parameter is the number of character from left of that field for example:
Left(name,3)
suppose that the name is equal to hello word than LEFT return hel
$this->db->delete('ea_appointments e1','ea_appointments e2');
in this section we create Aliases from main table name for easy to use.