Jump to content

ALTER TABLE, ORDER BY,.. keep all columns intact except 1...


lordvader

Recommended Posts

Let's say:

Primary Key | UID's | Additional Info 1 |  Additional Info 2

1 | 5 | foo5 | bar5

2 | 3 | foo3 | bar3

3 | 4 | foo4 | bar4

 

Primary Key is autoincrement

UID is non-sequential, but unique numbers

 

How do I sort this table so that UID is sequential again (lowest to highest) while keeping their associations with additional info 1,2, and keeping the primary keys where they are?

1 | 3 | foo3 | bar3

2 | 4 | foo4 | bar4

3 | 5 | foo5 | bar5

 

Thanks

Link to comment
Share on other sites

Hi,

 

try this: copy the structure of the table, execute query

INSERT INTO newTable SELECT
  NULL, `UID's`, `Additional Info 1`,  `Additional Info 2`
FROM oldTable ORDER BY ...

Delete old table, rename new table.

Link to comment
Share on other sites

I'm still learning mysql and the hard part is wrapping my head around all the nuances - every practice problem is so specific to the given example story that if I want to try variations, I'm lost because I don't know mysql's limitations and abilities.

Link to comment
Share on other sites

Is it a problem with mysql or just sql in general. It would seem to me that you are used to ISAM type databases or flat files and are trying to absorb how SQL itself works. If so, try a tutorial on SQL, it should help. Especially design.

 

Manuals usually just tell you how to use the features, not why you want to.

Link to comment
Share on other sites

I'm still learning mysql and the hard part is wrapping my head around all the nuances - every practice problem is so specific to the given example story that if I want to try variations, I'm lost because I don't know mysql's limitations and abilities.

Then explain what you actually want to do.

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.