Multiple Updates in MySQL

前端 未结 17 1023
南方客
南方客 2020-11-22 01:31

I know that you can insert multiple rows at once, is there a way to update multiple rows at once (as in, in one query) in MySQL?

Edit: For example I have the followi

相关标签:
17条回答
  • 2020-11-22 01:55

    You can alias the same table to give you the id's you want to insert by (if you are doing a row-by-row update:

    UPDATE table1 tab1, table1 tab2 -- alias references the same table
    SET 
    col1 = 1
    ,col2 = 2
    . . . 
    WHERE 
    tab1.id = tab2.id;
    

    Additionally, It should seem obvious that you can also update from other tables as well. In this case, the update doubles as a "SELECT" statement, giving you the data from the table you are specifying. You are explicitly stating in your query the update values so, the second table is unaffected.

    0 讨论(0)
  • 2020-11-22 01:56
    UPDATE table1, table2 SET table1.col1='value', table2.col1='value' WHERE table1.col3='567' AND table2.col6='567'
    

    This should work for ya.

    There is a reference in the MySQL manual for multiple tables.

    0 讨论(0)
  • 2020-11-22 01:56
    UPDATE `your_table` SET 
    
    `something` = IF(`id`="1","new_value1",`something`), `smth2` = IF(`id`="1", "nv1",`smth2`),
    `something` = IF(`id`="2","new_value2",`something`), `smth2` = IF(`id`="2", "nv2",`smth2`),
    `something` = IF(`id`="4","new_value3",`something`), `smth2` = IF(`id`="4", "nv3",`smth2`),
    `something` = IF(`id`="6","new_value4",`something`), `smth2` = IF(`id`="6", "nv4",`smth2`),
    `something` = IF(`id`="3","new_value5",`something`), `smth2` = IF(`id`="3", "nv5",`smth2`),
    `something` = IF(`id`="5","new_value6",`something`), `smth2` = IF(`id`="5", "nv6",`smth2`) 
    

    // You just building it in php like

    $q = 'UPDATE `your_table` SET ';
    
    foreach($data as $dat){
    
      $q .= '
    
           `something` = IF(`id`="'.$dat->id.'","'.$dat->value.'",`something`), 
           `smth2` = IF(`id`="'.$dat->id.'", "'.$dat->value2.'",`smth2`),';
    
    }
    
    $q = substr($q,0,-1);
    

    So you can update hole table with one query

    0 讨论(0)
  • 2020-11-22 01:57

    You may also be interested in using joins on updates, which is possible as well.

    Update someTable Set someValue = 4 From someTable s Inner Join anotherTable a on s.id = a.id Where a.id = 4
    -- Only updates someValue in someTable who has a foreign key on anotherTable with a value of 4.
    

    Edit: If the values you are updating aren't coming from somewhere else in the database, you'll need to issue multiple update queries.

    0 讨论(0)
  • 2020-11-22 01:58

    Why does no one mention multiple statements in one query?

    In php, you use multi_query method of mysqli instance.

    From the php manual

    MySQL optionally allows having multiple statements in one statement string. Sending multiple statements at once reduces client-server round trips but requires special handling.

    Here is the result comparing to other 3 methods in update 30,000 raw. Code can be found here which is based on answer from @Dakusan

    Transaction: 5.5194580554962
    Insert: 0.20669293403625
    Case: 16.474853992462
    Multi: 0.0412278175354

    As you can see, multiple statements query is more efficient than the highest answer.

    If you get error message like this:

    PHP Warning:  Error while sending SET_OPTION packet
    

    You may need to increase the max_allowed_packet in mysql config file which in my machine is /etc/mysql/my.cnf and then restart mysqld.

    0 讨论(0)
提交回复
热议问题