MYSQL help needed
#1

Hey

How is it possible to insert the lowest value with AUTO_INCREMENT?
I mean i have about 100 record but I deleted record with ID 2. Whenever I insert something it'll insert to ID 101 but i want to insert to ID 2. How i could solve this?
Reply
#2

i don't think you can.
Reply
#3

Hi
You can try this:

Код:
mysql_tquery(g_SQL,"SELECT u.Id + 1 AS FirstAvailableId FROM `yourtable` u LEFT JOIN `yourtable` u1 ON ul.Id = u.Id + 1 WHERE u1.Id IS NULL ORDER BY u.Id LIMIT 0, 1" , "sql_FirstNextID", "i",playerid);
Код:
forward sql_FirstNextID(playerid);
public sql_FirstNextID(playerid)
{
new i,query[128];
cache_get_value_name_int(0, "FirstAvailableId",i);
mysql_format(g_SQL, query,sizeof(query),"INSERT INTO `yourtable` (`id`) VALUES ('%i')",i);
mysql_tquery(g_SQL, query);
...
return 1;
}
Reply
#4

Southclaws explained everything very well.
Reply
#5

Quote:
Originally Posted by [HLF]Southclaw
Посмотреть сообщение
The real question is why? What would you gain from filling removed IDs? You'll probably cause more problems for yourself in future, just let AUTOINCREMENT do what it does and stay out. You have 4 billion possible integer values and 9,223,372,036,854,775,807 if your database supports 64 bit values (obviously SA:MP would not like that, but I'm making a point).

If you are relying on the IDs being in sequence - don't.
The inventory system listing the player item's with their DB ID-s and whenever an item dropped into the floor and picked up this ID is increasing. So the ID-s could be easily above a million that would disturb the inventory dialog and the problem is the inventory system is working always with theese ID-s (for example select the given ID when responding to the inventory listitem)

The inventory dialog will look like this:

ID / Item name / Amount
2342523 Fuel 0,3 l
2342524 Money $5000
2342525 Something 0,3 l
2342526 Something 0,3 l
Reply
#6

bump
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)