Jump to content

Reorder MySQL table


kashthealien

Recommended Posts

I have a MySQL table which looks like this

1 asd

2 sd

3 sad

4 sdg

 

when I delete the third element

it looks like this

1 asd

2 sd

4 sdg

 

How can I reorder this and make it look like

 

1 asd

2 sd

3 sdg

 

3 instead of 4?

Is there any inbuilt MySQL command?

SHould I use PHP ? If so how?

Please explain.

Thank you!

Link to comment
Share on other sites

lol, asking why is funny.

"UPDATE `table_name` `key` = 3 WHERE `key` = 4"

 

table_name is your table name and key is the field that has those numbers

 

Lol, I think he wants something a little more dynamic...  The only way I can think of doing this is to dump your data to a temporary table empty the current one and repopulate the original table from the temp one.  It will auto-increment everything back in order.  But there's no way to INSERT a record and have it "fill in the gaps" of the auto-increment field, at least not to my knowledge.

Link to comment
Share on other sites

In order to do that I believe you have to delete the table then recreate it and re-populate the data.

 

The id should not matter really, if you are a person who likes it to be in order, I would suggest you get used to it, cause it is not probable if you have 1,000 records and one gets deleted to go through and re-order every single record just to make it 1-1000. Imagine if you had 10,000 records and you deleted 100 records a day, wow thats a load on the server.

Link to comment
Share on other sites

The id should not matter really, if you are a person who likes it to be in order, I would suggest you get used to it, cause it is not probable if you have 1,000 records and one gets deleted to go through and re-order every single record just to make it 1-1000. Imagine if you had 10,000 records and you deleted 100 records a day, wow thats a load on the server.

 

It sure would be, but, you could always run a cron job late at night to dump and repopulate the tables.  kashthealien, what is the purpose of this, if you don't mind?

Link to comment
Share on other sites

I've thought about this before... there isn't any easy way to do so as there isn't a SQL command to do so and when you start looking at even as little as 100+ records even a server side process of some sort gets ridiculous.

"why" is funny to me because whether they be crazy, dumb, or just OCD... it doesn't matter what they will do with it, the challenge of "how" is more important.

Link to comment
Share on other sites

The reason we always ask why someone wants to do something that is normally not done, is because what they are trying to accomplish might be legitimate for their application, but the actual way of doing it would be to use a different method than what they are asking about.

Link to comment
Share on other sites

The reason we always ask why someone wants to do something that is normally not done, is because what they are trying to accomplish might be legitimate for their application, but the actual way of doing it would be to use a different method than what they are asking about.

 

Hmmm, I see.  kashthealien please describe your situation in a detailed manner so we can help you figure out a good solution.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.