alter/insert question

Discussion in 'Databases' started by zodiac, Aug 24, 2008.

  1. #1
    <?php
    mysql_query("alter table `categories` add column `order` varchar(3) NOT NULL default ''") or die(mysql_error());
    ?> 
    PHP:
    how can i insert a value in "order" based on id?
    id name order
    1 something 1
    3 something 2
    5 something 3
     
    zodiac, Aug 24, 2008 IP
  2. cont911

    cont911 Peon

    Messages:
    50
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I doubt someone can construct query so solve this completely in SQL.
    Anyway you should use application logic.
     
    cont911, Aug 24, 2008 IP
  3. zodiac

    zodiac Peon

    Messages:
    2,661
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    0
    #3
    are you saying it can't be done? :rolleyes:

    the problem at hand,it doesn't really matter.i can enter all 0 and it would be fine.
    just seeing if it can be done.
     
    zodiac, Aug 25, 2008 IP
  4. cont911

    cont911 Peon

    Messages:
    50
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I have time to think a little bit more about this task
    Solution is the following:

    - create one more table having the same structure as 'categories', but 'order' field should be autoincrement
    - copy content of current table by the sql below. it creates correct order assignment
    insert into `tmp_categories` (id, name)
    select id, name from `categories`
    order by id
    - delete content of categories table
    - copy content from tmp_categories to categories
     
    cont911, Aug 25, 2008 IP