I have the following working query:
$year = 2019;
$month = 6;
$stmt = $db->prepare('INSERT INTO officeRechNr (jahr,monat,zahl) VALUES (?,?,1) ON DUPLICATE KEY UPDATE zahl = LAST_INSERT_ID(zahl+1)');
$stmt->bind_param('ii', $year, $month);
$stmt->execute();
echo $db->insert_id;
The table officeRechNr
has the unique primary index ['jahr','monat']
and zahl
is an index
with autoincrement
.
So if the table officeRechNr
is empty, and I execute the code 5 times, then the output is
1, 2, 3, 4, 5
I tried to translate this in Laravel 5 with
\DB::select(DB::raw('INSERT INTO officeRechNr (jahr,monat,zahl) VALUES (?,?,1) ON DUPLICATE KEY UPDATE zahl = LAST_INSERT_ID(zahl+1)'),[2019,6]);
return DB::select('SELECT LAST_INSERT_ID()');
However, if I execute this code 5 times I get
0, 2, 3, 4, 5
Although it returns 0
after the first insert, there is a 1 in the zahl
column.
Why does my Laravel Code not return 1 after the first insert?