Jump to content

inserting a record in a specific place?


garyed

Recommended Posts

Is there a way to insert a record in a specific place in a table?

I have about 1000 records in the table I'm working on so lets say I want to add "Jack" in between "Alex" & "Suzie":

Name      Age

Jim          37

Gregg      29

Alex          42

Suzie        63

 

How would I do that?

 

 

Link to comment
Share on other sites

Unless you have a key which you can order by, there is no way of doing what you are wanting.

 

The only way would be to add a "DisplayOrder" column and then fill that in with the corresponding order and then when selecting out of your table, you would add "ORDER BY DisplayOrder" to your query.

 

~awjudd

Link to comment
Share on other sites

I'm really kind of shocked that you can't just insert a record after or before another record.

This problem came about because I was adding the name of some Canadian villages to an existing table in alphabetical order & for some rerason they were not being inserted to the end of the database but about 10 records before the end.  Evidently I deleted about five records in that spot so every time I inserted a new record it went back to the place where the old ones were deleted. What I finally did was insert five dummy records & now when I insert a record it goes at the end of the table.  When I get through with all the entries I'm going to delete the dummy ones & see if that works O.K.

 

 

Link to comment
Share on other sites

The way in which data is entered into a database is not particularly important provided each row can be identified independently with a primary key. If you want to order something alphabetically you should use the "ORDER BY" syntax in your SQL statement. Ordering by the village name in an ascending order with the "ASC" syntax will give you the statements in an alphabetical order starting with A; "DESC" will obviously reverse this.

 

Don't get hung up on the order in which your data appears on your screen when you haven't ordered it. Doing this will cause serious aggravation when dealing with 10,000 rows or even worse a million.

Link to comment
Share on other sites

Actually what I'm trying to do is populate a drop down menu with a bunch of US or Canadian cities & villages after a particular state or province has been selected.

 

<option value="" selected="selected" style="width:250px;" > ----  </option> 
<?php
// This will populate the drop down menu with the database entries for cities
while ($city_row=mysql_fetch_array($new_city))
{
$state=$city_row["state"];
$city=$city_row["city"];  
?>
<option value="<?php echo $city; ?>" style="width:250px;"> <?php echo $city; ?> </option>
<?php
}
?>

 

 

If they are already in alphabetical order the above code works fine.

If I learn how to use ORDER BY will it work in this instance if they were not in alphabetical order already?

 

Link to comment
Share on other sites

If your looking at normalising your database you should separate state and city as the state can be derived from the city, unless you've already done that? When they select a state just filter for all the cities by that state and order it alphabetically with the ORDER BY syntax.

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.