Jump to content
MakeWebGames

Mysql 500 + queries?


Recommended Posts

Ok so I need to put in some IDS into my 'free_ids' table. I found the mod on CE but it only started from the 2000's and I have about 500 or so under 2000 that are missing. I really need help with this because I don't want to sit there typing "insert into free_ids values 5" then 6 then 7 etc. BTW There are stuff like 20-30, 36-67, 89-199 so I need a way to do that. Just something like in excel it's D6:D10

Screenshot added.

2vchmxh.jpg

Link to comment
Share on other sites

Use a for loop between the values you want to insert for (eg. 1-2000)

Use your regular insert query for each ID, and add this to the end of the query:

WHERE NOT EXISTS(SELECT `ID` FROM `players` WHERE `id`=$i)

So something like this:

INSERT INTO `free_ids` (`id`) VALUES ($id) WHERE NOT EXISTS(SELECT `ID` FROM `players` WHERE `id`=$i);

Where $i is the ID that you are inserting.

This isn't very efficient as it will still be querying the database for every single ID even if nothing's being inserted. But it's a lazy way to do it, and if you don't mind that it takes some time.

Link to comment
Share on other sites

What Zeggy said.

Or, for the more mysql illiterate:

$x=HighestIDYouWantToGoTo;

$i=0;

while($i<$x)

{

$fullID=$db->num_rows($db->query("SELECT userid FROM users WHERE userid=$i"));

if(!$fullID){$db->query("INSERT INTO free_ids VALUES($i)");}

$x++;

}

Not nearly as efficient as Zeggy's would be, but as you're only running it once shouldn't really matter.

Link to comment
Share on other sites

What Zeggy said.

Or, for the more mysql illiterate:

$x=HighestIDYouWantToGoTo;

$i=0;

while($i<$x)

{

$fullID=$db->num_rows($db->query("SELECT userid FROM users WHERE userid=$i"));

if(!$fullID){$db->query("INSERT INTO free_ids VALUES($i)");}

$x++;

}

Not nearly as efficient as Zeggy's would be, but as you're only running it once shouldn't really matter.

This just inserted about 200,000 id 0's

Link to comment
Share on other sites

All that is not very smart... If you have an auto_increment table primary key (as I suspect) you may set the value you want for the next row, and it will then continue from there. Without the need to have empty or useless rows in betwee:

[mysql]

ALTER TABLE tbl AUTO_INCREMENT = 100;

[/mysql]

BTW that was a 10 sec google search ;)

Link to comment
Share on other sites

All that is not very smart... If you have an auto_increment table primary key (as I suspect) you may set the value you want for the next row, and it will then continue from there. Without the need to have empty or useless rows in betwee:

[mysql]

ALTER TABLE tbl AUTO_INCREMENT = 100;

[/mysql]

BTW that was a 10 sec google search ;)

If you set the auto_increment value to something lower than MAX (`id`)+1, it will automatically be reset to MAX (`id`)+1. Auto_increment doesn't fill up gaps from deleted rows. It starts counting from at least the largest auto_increment value in your table.

 

If you don't have an auto_increment (though why you wouldn't is beyond me), you can use LAST_INSERT_ID().

It's a good MySQL function that works similar, in principal, to an auto_increment

Actually last insert ID requires an auto_increment column, and only returns the ID from the last inserted row during that connection. Meaning it will only be useful if you've just inserted a row on the same page.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...