Mysql
 sql >> Base de données >  >> RDS >> Mysql

Incrémenter automatiquement un varchar dans une requête sql

Essayez ceci :

<?php
// fetch the very last entry

$Barcode = 'PU000001234567';

preg_match("/(\D+)(\d+)/", $Barcode, $Matches); // Matches the PU and number

$ProductCode = $Matches[1];

$NewID = intval($Matches[2]);
$NewID++;


$BarcodeLength = 12;
$CurrentLength = strlen($NewID);
$MissingZeros = $BarcodeLength - $CurrentLength;

for ($i=0; $i<$MissingZeros; $i++) $NewID = "0" . $NewID;

$Result = $ProductCode . $NewID;

echo $Result;

// insert into database with $Result