Control of the numerical order of certain entries in line
-
How can certain entries in the lines of table SQL be monitored?
Like we have a table of goods. If we add the goods, the serial number rises if it already exists. If we remove the product that is, the serial number is dropping. How do you do that?
1♪ Do you want me to write a procedure with the triggers at MySQL?
2♪ Do it at php?
3♪ Or maybe there's some sort of prepped function in SQL or a way to control it?
This is an example:
We've already added something.
| порядковые номера | товары | |-------------------|--------| | 1 |ведро | |-------------------|--------| | 2 |ведро | |-------------------|--------| | 1 |грабли | |-------------------|--------| | 3 |ведро | |-------------------|--------| | 2 |грабли | |-------------------|--------| и.т.д.
TABLES - We used to add, we took out one of the buckets. And if we add one bucket again, the serial number of the bucket will be 3 again. If we can add a bucket, after the third bucket, then the next bucket number will be four.
| порядковые номера | товары | |-------------------|--------| | 1 |ведро | |-------------------|--------| | 2 |ведро | |-------------------|--------| | 1 |грабли | |-------------------|--------| | 2 |грабли | |-------------------|--------|
-
Triggers don't need. Somehow:
insert into ТАБЛИЦА_ТОВАРОВ select (select coalesce(max("Порядковые номера"),0)+1 from ТАБЛИЦА_ТОВАРОВ where "Товары" ='ведро'),'ведро';
Instead of a bucket in two places, put your merchandise.