Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with a value replacement query

    Hey guys, i'm sorry to bother you, im not really that proficient at sql but im desperatly needing a query that solves the following:

    I have two tables with the following structure:

    Table 1
    ---------
    id | Name
    ---------
    1 | John
    2 | Debbie
    3 | Kim
    4 | Mary

    Table 2
    ---------
    id | Name
    ---------
    654 | John
    415 | Debbie
    68 | Kim
    289 | Mary

    Table 2 id's values are all messed up and I need to replace them so that they look exactly like in Table one, my guess is that query will have to replace value of "Id" based on the "Name" column, I hope I made myself clear.

    Thanks in advance.

  • #2
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,633
    Thanks
    4
    Thanked 148 Times in 139 Posts
    What have you tried?

  • #3
    New Coder
    Join Date
    Aug 2012
    Location
    Odessa, Ukraine
    Posts
    16
    Thanks
    0
    Thanked 4 Times in 4 Posts
    I assume that:
    1. [Name] is unique in both tables.
    2. [Id] doesn't have any constraints, i.e. can be UPDATEd.
    3. You won't run this on a production, because the code is off the top of my head, I didn't test it in any way.


    Code:
    UPDATE Table2
    SET ID = (SELECT ID FROM Table1 WHERE Table1.Name = Table2.Name)

  • #4
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Bismark View Post
    I assume that:
    1. [Name] is unique in both tables.
    2. [Id] doesn't have any constraints, i.e. can be UPDATEd.
    3. You won't run this on a production, because the code is off the top of my head, I didn't test it in any way.


    Code:
    UPDATE Table2
    SET ID = (SELECT ID FROM Table1 WHERE Table1.Name = Table2.Name)
    This did the job, thank you!


  •  

    Posting Permissions

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