Mysql

如何在 MySQL 中獲取下一個自增 id?

  • February 20, 2018

如何在 mysql 中獲取下一個 id 以將其插入表中?

INSERT INTO payments (date, item, method, payment_code)
VALUES (NOW(), '1 Month', 'paypal', CONCAT("chaminda", id))

這是一種方法。

Step 1: Insert without payment_code.

Step 2: Get mysql_insert_id from STEP 1.

Step 3: Update payment_code with where condition as mysql_insert_id.

PHP 中的程式碼片段: 可能需要更正命令。

1.    mysql_query("INSERT INTO payments (date, item, method, payment_code)
   VALUES (NOW(), '1 Month', 'paypal')");

2.     printf("Last inserted record has id %d\n", mysql_insert_id());

3.     mysql_query(“UPDATE payments set payment_code=CONCAT("chaminda",mysql_insert_id()) where id=mysql_insert_id());

引用自:https://dba.stackexchange.com/questions/175861