Multiple Updates in MySQL

前端 未结 17 1006
南方客
南方客 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.

提交回复
热议问题