dongyu3967 2012-03-07 06:24
浏览 141
已采纳

Codeigniter:使用insert_batch()检索insert_ids

Is there a way to grab all the insert ids ( using $this->db->insert_id() ) from a previously run insert_batch() method?

Ideally it will spit out a simple array of the ids in the order they were inserted.

  • 写回答

1条回答 默认 最新

  • drfif48428 2012-03-07 10:12
    关注

    creating a trigger would work.

    not sure if its gona affect performance on huge batch insertion

    DELIMITER $$
    
    CREATE
        TRIGGER `test`.`getids` AFTER INSERT
        ON `database_name`.`table_name`
        FOR EACH ROW BEGIN
            INSERT INTO last_inserted_ids (last_insertId) VALUES(LAST_INSERT_ID());
    
        END$$
    
    DELIMITER ;
    

    it will get all the ids into the table, as you want them in an array write a query which run exactly after the batch and gets all the values from table last_inserted_ids and then truncate it so that you always have the desired ids after a batch is executed.

    hope this helps your case.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?