-- 更新用户表,当id=1设置address='北京',当id=2设置address='上海',当id=3设置address='南京' UPDATEuser SET address =CASE id WHEN1THEN'北京' WHEN2THEN'上海' WHEN3THEN'南京' END WHERE id IN (1,2,3)
2.2 批量更新多个字段
语法
UPDATE table_name SET field_name1 =CASE where_field_name WHEN1THEN'value1' WHEN2THEN'value2' WHEN3THEN'value3' END, field_name2 =CASE where_field_name WHEN1THEN'value-1' WHEN2THEN'value-2' WHEN3THEN'value-3' END WHERE where_field_name IN (1,2,3)
示例
-- 更新用户表,当id=1设置address='北京'phone=17600113410.... UPDATEuser SET address =CASE id WHEN1THEN'北京' WHEN2THEN'上海' WHEN3THEN'南京' END, phone =CASE id WHEN1THEN'17600113410' WHEN2THEN'17600113411' WHEN3THEN'17600113412' END WHERE id IN (1,2,3)