Jump to content

[SOLVED] how to fill missing index rows?


mameha

Recommended Posts

I have data as follows:

 

id | name

0 | john

1 | bob

3 | dave

4 | ralph

5 | david

7 | sam

9 | james

 

As you can see, the 'id' column has many missing values.

 

I want to autofill all the missing values so data becomes like this:

 

 

id | name

0 | john

1 | bob

2 | NULL

3 | dave

4 | ralph

5 | david

6 | NULL

7 | sam

8 | NULL

9 | james

 

Is there some command that can easily do this in SQL?  (MySQL5)

Link to comment
Share on other sites

Its a little tricky to explain.

 

I am importing about 5000 records to another system.

 

The new system will create new keys for each record, therefore 'dave' above will become id=2 on the new system unless I put in a blank record for id2. 

 

I need to avoid this happening because people will expect the same id numbers for the records.

Link to comment
Share on other sites

What database is on the system you want to pull from?  Can you export the data to a common format such as CSV, or something?  You could even write code to create the mysql statement to copy paste if you wanted.  (all this should/would keep the data intact).

 

What thorpe was hinting at is why would you have missing data in an auto increment field?  IDs that are no longer used should at least have an "enabled?" column, and simply be disabled when no longer needed afaik.

Link to comment
Share on other sites

fixed with:

 

$i=0;
while ($i<6000){

		$query = "SELECT title FROM `table` WHERE id = '".$i."'";
		$result = mysql_query($query);
		if(mysql_num_rows($result) != 1) {
			// insert new
			$query = "INSERT INTO `table` ( `id` , `entry_date`, `title`, `status_name`, `vip_mark` ) VALUES ('".$i."', '2008-10-28 01:00:00', 'NULL', 'Completed', '0');";			
			echo $i."<br/>";
		}
		$result = mysql_query($query);

    			$i++;
} 

echo 'done';

Link to comment
Share on other sites

edit, I don't see why you're filling null spots .. it seems unnecessary since you can set the auto index...

------------

I'm going to say use "mysqldump" (http://dev.mysql.com/doc/refman/5.0/en/mysqldump.html).

However I can't be more helpful since I use phpmyadmin and I don't see the commands it uses behind the scenes.

 

This should give you something that you could copy/paste into your new database, keeping the data in order.  Obviously you wouldn't delete the old data until you verified it was successful.

 

Otherwise save it to CSV and import it into mysql.

 

 

If you choose the first option, is should keep the auto index in the correct spot so as not to fill in previous deleted ids.  You can always alter the table to reflect the auto index you want as well.

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.