php - Mysql,如何读取同一插入行的其他字段中的自增值?

标签 php mysql database insert auto-increment

您好,我需要在交易的评论字段中提供交易 ID

mysql_query("INSERT INTO `transactiontb` (`tid`, `amount`, `comment`) VALUES (NULL,'$amount', CONCAT('Transaction # ',`tid`)')");

我该怎么做?

最佳答案

  1. 摆脱 mysql_* 函数。查看 MySQLi 或 PDO。
  2. 不要存储 tid 两次。为什么在选择它时不连接而不是这样存储呢?这不是最好的方法。

作为引用,请尝试LAST_INSERT_ID()+1):

INSERT INTO `transactiontb` (`tid`, `amount`, `comment`)
VALUES (NULL, '$amount', CONCAT_WS('Transaction # ', LAST_INSERT_ID()+1))

LAST_INSERT_ID() 将给出前一个 INSERT 的 ID,而不是当前 INSERT 的 ID,因此您必须添加 1。

http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id

The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned by the function to a given client is the first AUTO_INCREMENT value generated for most recent statement affecting an AUTO_INCREMENT column by that client. This value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior ensures that each client can retrieve its own ID without concern for the activity of other clients, and without the need for locks or transactions.

关于php - Mysql,如何读取同一插入行的其他字段中的自增值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27925926/

相关文章:

sql - 跟踪表中值更新的时间

php - 如果 CURLOPT_SSLCERT 设置为同时包含证书和私钥的文件,我是否应该设置 CURLOPT_SSLKEY?

mysql - 无法在单个 Controller 中运行多个 GET 方法

php - Magento 图片上传文件名不保存在数据库中

php - (php)导入.sql并添加列(如果表存在)或创建新表

mysql - 分层数据库菜单

php - MySQL:聚合和分组

php - 如何在 PHP 中检查字符串在 UTF8 中是否只有国际字母和空格?

php - mySQL另一种组合查询

php - 使用 %C3 特殊字符时数据输入 mySQL 被截断