As suggested in the comments, do consider that approach. Simply have 2 columns, and bind them through UNIQUE so there's no conflicts. If you look for the 1st id in Store ID 10, simply WHERE store_id = 10 AND other_id = 1
. It's more logical, and you can make a simple function to output this as 100001
:
function store_string($int_store_id, $int_other_id) {
$str = str_repeat('0', (2 - strlen($int_store_id))).$int_store_id;
$str .= str_repeat('0', (3 - strlen($int_other_id))).$int_other_id;
return $str;
}
(PHP example, but simply look up strlen and str_repeat to get the idea.
This gives you a lot of advantages such as easier searching for either value, and the possibility to go beyond store_id
99 without having to alter all existing rows and just the output function.
Regarding the actual INSERT
, you can run your inserts like this:
INSERT INTO table_name (store_id, other_id, third_value)
SELECT {$store_id}, (other_id + 1), {$third_value}
FROM (( SELECT other_id
FROM table_name
WHERE store_id = {$store_id}
ORDER BY other_id DESC)
UNION ALL
( SELECT '0')
LIMIT 1) AS h
And simply extend with more values the same way $third_value
is used.