Multiple Updates in MySQL

前端 未结 17 999
南方客
南方客 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:31

    Since you have dynamic values, you need to use an IF or CASE for the columns to be updated. It gets kinda ugly, but it should work.

    Using your example, you could do it like:

    UPDATE table SET Col1 = CASE id 
                              WHEN 1 THEN 1 
                              WHEN 2 THEN 2 
                              WHEN 4 THEN 10 
                              ELSE Col1 
                            END, 
                     Col2 = CASE id 
                              WHEN 3 THEN 3 
                              WHEN 4 THEN 12 
                              ELSE Col2 
                            END
                 WHERE id IN (1, 2, 3, 4);
    
    0 讨论(0)
  • 2020-11-22 01:33

    There is a setting you can alter called 'multi statement' that disables MySQL's 'safety mechanism' implemented to prevent (more than one) injection command. Typical to MySQL's 'brilliant' implementation, it also prevents user from doing efficient queries.

    Here (http://dev.mysql.com/doc/refman/5.1/en/mysql-set-server-option.html) is some info on the C implementation of the setting.

    If you're using PHP, you can use mysqli to do multi statements (I think php has shipped with mysqli for a while now)

    $con = new mysqli('localhost','user1','password','my_database');
    $query = "Update MyTable SET col1='some value' WHERE id=1 LIMIT 1;";
    $query .= "UPDATE MyTable SET col1='other value' WHERE id=2 LIMIT 1;";
    //etc
    $con->multi_query($query);
    $con->close();
    

    Hope that helps.

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

    Not sure why another useful option is not yet mentioned:

    UPDATE my_table m
    JOIN (
        SELECT 1 as id, 10 as _col1, 20 as _col2
        UNION ALL
        SELECT 2, 5, 10
        UNION ALL
        SELECT 3, 15, 30
    ) vals ON m.id = vals.id
    SET col1 = _col1, col2 = _col2;
    
    0 讨论(0)
  • 2020-11-22 01:40

    Yes, that's possible - you can use INSERT ... ON DUPLICATE KEY UPDATE.

    Using your example:

    INSERT INTO table (id,Col1,Col2) VALUES (1,1,1),(2,2,3),(3,9,3),(4,10,12)
    ON DUPLICATE KEY UPDATE Col1=VALUES(Col1),Col2=VALUES(Col2);
    
    0 讨论(0)
  • 2020-11-22 01:40

    The following will update all rows in one table

    Update Table Set
    Column1 = 'New Value'
    

    The next one will update all rows where the value of Column2 is more than 5

    Update Table Set
    Column1 = 'New Value'
    Where
    Column2 > 5
    

    There is all Unkwntech's example of updating more than one table

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

    And now the easy way

    update my_table m, -- let create a temp table with populated values
        (select 1 as id, 20 as value union -- this part will be generated
         select 2 as id, 30 as value union -- using a backend code
         -- for loop 
         select N as id, X as value
            ) t
    set m.value = t.value where t.id=m.id -- now update by join - quick
    
    0 讨论(0)
提交回复
热议问题