如何检索由 MySQL 事件插入的记录的 ID

how to retrieve id of record that is inserted by MySQL event

以下是我的代码:

$schedule = $this->db->query("CREATE EVENT ".$data['id']." ON SCHEDULE 
EVERY 1 MONTH ON COMPLETION PRESERVE ENABLE DO INSERT INTO oops_invoice 
(id, prefix, number, bill, account_id, order_delivery_id, date, staff_id, 
customer_note, recurrence, status, itp, utp) VALUES ('', '".$data['prefix']."', 
'".$data['number']."', '".$data['bill']."', '".$data['account_id']."', NULL, 
'".$data['date']."', NULL, '".$data['customer_note']."', '".$data['recurrence']."', 
'Draft', '".$data['itp']."', CURRENT_TIMESTAMP)");

$this->db->query("SET GLOBAL event_scheduler =  'ON'");

我坚持使用 id,如何检索由 MySQL 事件插入的 id od 行..

我试过以下但在 table 为空时不起作用...

除了 table 为空的一种情况外,以下代码工作正常....当时它 return 什么都没有,所以我被卡住了...

这个与我们插入的简单数据不同,这个使用 MySQL 事件插入,所以 $this->db->insert_id(); 不起作用......

$last = $this->db->query('SELECT MAX(id) AS `maxid` FROM `oops_invoice`')->row()->maxid;
$this->db->lastInsertId()

一定是您的解决方案

不需要检索ID, 您可以像这样在创建事件的 1 或 2 分钟之前开始事件... 它将消除 table.

中的 2 个插入
CREATE EVENT ".$data['id']." ON SCHEDULE EVERY 1 MONTH STARTS DATE_SUB(NOW(),INTERVAL '0:01' HOUR_MINUTE) ON COMPLETION PRESERVE ENABLE DO INSERT INTO oops_invoice (id, prefix, number, bill, account_id, order_delivery_id, date, staff_id, customer_note, recurrence, status, itp, utp) VALUES ('', '".$data['prefix']."', '".$data['number']."', '".$data['bill']."', '".$data['account_id']."', NULL, '".$data['date']."', NULL, '".$data['customer_note']."', '".$data['recurrence']."', 'Draft', '".$data['itp']."', CURRENT_TIMESTAMP)");
    $this->db->query("SET GLOBAL event_scheduler =  'ON'");