UPDATE multiple tables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    UPDATE multiple tables

    The following query produces a very slow running failure. (Usually time out).
    I am not sure about the syntax. Any ideas?
    Code:
    UPDATE `table1`,`table2`,`table3` SET [all fields in all three tables to values]
     WHERE `tables1`.`id` = '20439'
  • Motoma
    Recognized Expert Specialist
    • Jan 2007
    • 3236

    #2
    Originally posted by code green
    The following query produces a very slow running failure. (Usually time out).
    I am not sure about the syntax. Any ideas?
    Code:
    UPDATE `table1`,`table2`,`table3` SET [all fields in all three tables to values]
     WHERE `tables1`.`id` = '20439'
    The EXPLAIN syntax should help you out here.
    What is happening is that you are doing a 3 table join, without realizing it. You are updating tables 2 and 3 without any restrictions; all entries in these two tables are updates all the time.

    In order to see what exactly you are updating, do a select on the same set:
    Code:
    SELECT * FROM  `table1`,`table2`,`table3`
    WHERE `tables1`.`id` = '20439'
    Another thing causing a problem is that you have a string as your ID. If that really is an integer in your table, you don't need the '' around it.

    Comment

    • code green
      Recognized Expert Top Contributor
      • Mar 2007
      • 1726

      #3
      Another thing causing a problem is that you have a string as your ID. If that really is an integer in your table, you don't need the '' around it.
      Thanks. Didn't realise I had done that.
      What is happening is that you are doing a 3 table join, without realizing it. You are updating tables 2 and 3 without any restrictions; all entries in these two tables are updates all the time.
      Right so I need
      Code:
      UPDATE `table1`,`table2`,`table3` 
      SET [all fields in all three tables to values]
       WHERE `tables1`.`id` = 20439 AND `tables2`.`id` = 20439 
      AND `tables3`.`id` = 20439
      So if the ID in one table does not exist the whole query will fail. I wanted to UPDATE the tables WHEREVER the ID appeared. This idea doesn't look possible without using IF EXISTS and sub queries checking for the ID in each table. What do you recommend?

      Comment

      • Motoma
        Recognized Expert Specialist
        • Jan 2007
        • 3236

        #4
        Originally posted by code green
        Thanks. Didn't realise I had done that.
        Right so I need
        Code:
        UPDATE `table1`,`table2`,`table3` 
        SET [all fields in all three tables to values]
         WHERE `tables1`.`id` = 20439 AND `tables2`.`id` = 20439 
        AND `tables3`.`id` = 20439
        So if the ID in one table does not exist the whole query will fail. I wanted to UPDATE the tables WHEREVER the ID appeared. This idea doesn't look possible without using IF EXISTS and sub queries checking for the ID in each table. What do you recommend?

        You could use ORs in place of ANDs, but that will slower than hell. You should just use 3 queries.
        Last edited by Motoma; Apr 10 '07, 12:45 PM. Reason: language...

        Comment

        • code green
          Recognized Expert Top Contributor
          • Mar 2007
          • 1726

          #5
          I've decided to use four. Three queries SELECT 1 FROM `table` WHERE `id` = 20439 for each of the three tables which should be fast and gives me the chance to catch any missing ids at this point. Then construct an UPDATE query live time as a php string using only the tables where the id was found. Thanks for your help Motoma.

          Comment

          • Motoma
            Recognized Expert Specialist
            • Jan 2007
            • 3236

            #6
            Glad I could help. Come back any time.

            Comment

            • gelmo214
              New Member
              • Jul 2010
              • 6

              #7
              SIr Code green?

              Can you share the code structure that you use in that 3 tables?..thanks ...

              Comment

              • gelmo214
                New Member
                • Jul 2010
                • 6

                #8
                re: UPDATE multiple tables
                I've decided to use four. Three queries SELECT 1 FROM `table` WHERE `id` = 20439 for each of the three tables which should be fast and gives me the chance to catch any missing ids at this point. Then construct an UPDATE query live time as a php string using only the tables where the id was found. Thanks for your help Motoma
                >>>>>>>How can i apply those 3 select and UPDATE query in VB.net?thanks..

                Comment

                Working...