Is it possible to use multiple tables in an UPDATE query?

Discussion in 'PHP' started by x0x, Feb 16, 2010.

  1. #1
    I know how to use multiple tables with SELECT queries, but what about UPDATE queries?

    mysql_query("UPDATE users SET msg=msg+1 WHERE userdata.did='$var' AND id!=$id");


    - does not work. Basically I need to update the users table, but only the rows that match 'did' in table userdata with the given $var variable.

    users and userdata have no other connection... actually users.yid matches userdata.id - but that's how it was in the old query (userdata.id was a variable) - but I now need to pull all rows from userdata where userdata.did='$var' (users.uid and userdata.id are different... Wow, I'm not surprised if you don't understand a thing I tried to say!


    The actual query is a lot more complex, but I broke it down, so it's easier to understand. I just need to get the general idea. Is it even possible? My second option is a php loop.
     
    Last edited: Feb 16, 2010
    x0x, Feb 16, 2010 IP
  2. kingsoflegend

    kingsoflegend Well-Known Member

    Messages:
    202
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    108
    #2
    You can use joins to do that.

    UPDATE table1 JOIN table2 ON table1.column1=table2.column2 SET table1.columna='test', table2.columnb='test' WHERE (condition)
     
    kingsoflegend, Feb 16, 2010 IP
  3. Izonedig

    Izonedig Member

    Messages:
    150
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #3
    Yes, join may work for you well.
     
    Izonedig, Feb 16, 2010 IP