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.

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

报告相同问题?

悬赏问题

  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败