简体   繁体   English

从数据库 mySQL PHP 生成自动事务 ID

[英]Generate auto transaction id from database mySQL PHP

I am trying to generate the transaction number while I am inserting the data in my database through PHP.当我通过 PHP 在我的数据库中插入数据时,我正在尝试生成事务号。 I want to generate the sequence transaction id, so that I am doing the below SQL.我想生成序列事务id,所以我在做下面的SQL。 I am counting the ID column and add +1 value for new inserts.我正在计算 ID 列并为新插入添加 +1 值。 but when I have a bulk insert it will not for after a single entry and showing different transaction id.但是当我有一个批量插入时,它不会在单个条目之后显示不同的事务 ID。

I want the ALFABHETs will always constant and after the number will continuously run.我希望ALFABHETs始终保持不变,并且在数字将持续运行之后。 if the number will be reached then, it will automatically become 3 to 4 digits.如果达到该数字,它将自动变为3到4位数字。

For example, the number will start from MG001 .例如,编号将从MG001开始。 If MG999 then it will becomes to MG1000 , MG1001 , MG1002 etc如果是 MG999 则将变为MG1000MG1001MG1002

#table_name: table1

ID | transactionID  
------------------
1  | MG001
2  | MG001
3  | MG003
4  | MG004
5  | MG004
6  | MG001

$stmt = $con->prepare("SELECT * FROM `table1` ORDER BY `ID` DESC LIMIT 1");
$stmt->execute();
if ($stmt->rowCount() > 0) {
    while ($data = $stmt->fetch()) {
        $transactionCode = 'MG' . $data['ID'] + 1;
    }
} else {
    $transactionCode = 'MG001' . '1';
}

Your query loads the record with the highest ID, which is a single record and adds 1 to it if found.您的查询会加载 ID 最高的记录,这是一条记录,如果找到,则向其添加 1。 If there was no record, then you use 'MG0011' as value.如果没有记录,则使用'MG0011'作为值。 Since you have a single value, if you put that into your bulk insert's code, then it will be duplicated.由于您只有一个值,因此如果将其放入批量插入的代码中,那么它将被复制。 So you should load transactionID (and not ID), strip the MG or whatever prefix from it and add 1 to it.所以你应该加载transactionID(而不是ID),去掉MG或任何前缀,然后加1。 Example:例子:

$stmt = $con->prepare("SELECT * FROM `table1` ORDER BY `ID` DESC LIMIT 1");
$stmt->execute();
if ($stmt->rowCount() > 0) {
    while ($data = $stmt->fetch()) {
        $transactionCode = (int)substr($data['transactionID'], 2) + 1;
    }
} else {
    $transactionCode = 1;
}

The code above generates a starting point and does not care (yet) about the format that you intend to use.上面的代码生成了一个起点,并且(还)不关心您打算使用的格式。 Now, let's implement a function that generates the code as you need it:现在,让我们实现一个 function 来根据需要生成代码:

function generateTransactionCode($input, $prefix = 'MG') {
    while (strlen($input) < 3) $input = '0'.$input;
    return $prefix.$input;
}

And now let's generate a bulk insert command (I assume that you auto_increment the ID ):现在让我们生成一个批量插入命令(我假设您auto_increment增加ID ):

$sql = 'insert into table1(transactionID) values';
$values = [];
for ($index = 0; $index < 5; $index++) { //we insert 5 values in this example
    $values[]='('.generateTransactionCode($transactionCode + $index).')';
}
$sql .= implode(',', $values);
//Execute the insert

It's more tricky to do that with insert select statements.使用插入 select 语句来做到这一点更加棘手。 If you need to do that, then you will need to use row_number .如果您需要这样做,那么您将需要使用row_number

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM