umysql update using 2 tables

Discussion in 'MySQL' started by schlogo, Mar 4, 2013.

  1. #1
    Hello

    I fighting mysql and could use some help , i am not doing it correctly

    I have 2 tables :

    table1 contains :
    title
    created_by

    second table :
    userName
    userID

    The titles are the same but the id are differents. What i want to do is compare the title fields of the 2 tables and each time they match, replace the id of the first table with the id of the second one

    I used this but it failed

    UPDATE table1
        SET created_by = {SELECT table2.userID
                  FROM table2
                  WHERE table2.userName = table1.title)
        WHERE EXISTS (SELECT table2.userID
                              table2
                              WHERE table2.userName = table1.title);
    Code (markup):


    Thanks for your help
     
    Solved! View solution.
    schlogo, Mar 4, 2013 IP
  2. #2
    hope this query will help you.

    update table1 as t1,table2 as t2 set t1.created_by=t2.userID where t1.title=t2.userName;
    Code (markup):
    regards,

    --amrush
     
    amrush, Mar 5, 2013 IP
    schlogo likes this.
  3. schlogo

    schlogo Well-Known Member

    Messages:
    1,615
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    140
    #3
    Amrush, tks a million. You rocked on this one !
     
    schlogo, Mar 5, 2013 IP
  4. amrush

    amrush Active Member

    Messages:
    30
    Likes Received:
    2
    Best Answers:
    2
    Trophy Points:
    58
    #4
    You are welcome :)

    regards,

    --amrush
     
    amrush, Mar 5, 2013 IP