Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2012
    Posts
    3

    Unanswered: Help with a value replacement query

    Hey guys, i'm sorry to bother you, i have very poor sql scripting knowledge 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. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    update table2
       set id = t1.id
      from table2 t2
    inner
      join table1 t1
        on t1.name = t2.name
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Aug 2012
    Posts
    3
    Thanks!, by t1 and t2 you mean table1 and table2? cause I don't quite get that part.

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    t1 and t2 are table aliases
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    Join Date
    Aug 2012
    Posts
    3
    Tried that but didn't work though this did the job:

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

    thank you anyways

  6. #6
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by yashirotcm View Post
    Tried that but didn't work
    sorry, i am not familiar with the "didn't work" error message
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  7. #7
    Join Date
    Aug 2012
    Posts
    30
    Quote Originally Posted by yashirotcm View Post
    Tried that but didn't work though this did the job:

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

    thank you anyways
    It will work if you do it properly

    or atleast tell us what happened when you used it?

Posting Permissions

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