SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Dublin
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to insert 2 new columns based on value in existing column in DB

    I want to insert 2 new columns to an already existing/populated table in my database.

    I have 2 columns in an excel file, the columns are called 'weight' and 'color'. The condition is that the values in both these columns are associated with the value in a column called 'model_number' (this is unique, the value in this column is not replicated).
    The column 'Model_Number' is also in the excel file.

    My excel file is organised in such a way that it should be easy enough to execute this query.

    Excel file sample:
    Column Weight_ _ _ _ _ _ _ _ _ Column Color_ _ _ _ _ _ _ _ _ Column Model_Number (this column is already in DB)
    3kg_ _ _ _ _ _ _ _ __ _ _ _ _ _ _Red_ _ _ _ _ _ _ _ __ _ _ _ _ 4545
    5kg_ _ _ _ _ _ _ _ __ _ _ _ _ _ _Green_ _ _ _ _ _ _ _ __ _ _ _ 8789
    2KG_ _ _ _ _ _ _ _ __ _ _ _ _ _ _Blue_ _ _ _ _ _ _ _ __ _ _ _ _7987
    3KG_ _ _ _ _ _ _ _ __ _ _ _ _ _ _Blue_ _ _ _ _ _ _ _ __ _ _ _ _4332

    So for example where the value in Column 'Model_Number' is 4545 the value 3kg will be added to Column Weight and the value Red will be add to Column Color in the DataBase.

    What SQL code do I need to use here?
    Do I have to add the 2 new columns first before adding the values?
    Also I am concerned that by inserting these 2 new columns and adding values that I will reset the 'timestamp' value column in each row which I don't want to do. Is there a way to keep the original timestamp value?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,273
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    since you mention a timestamp column, i'm going to guess that this is mysql

    could you please do a SHOW CREATE TABLE for the table
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •