Home  >  Article  >  Database  >  How to update multiple fields at the same time using sql?

How to update multiple fields at the same time using sql?

青灯夜游
青灯夜游Original
2019-05-15 15:23:0331846browse

How to update multiple fields at the same time using sql?

In SQL, the update statement is used to update data. There are many ways to update multiple field statements at once in SQL. Here are two ways to introduce them to you. I hope Helpful to everyone.

Method 1:

UPDATE 表名 SET 字段1=值,字段2=值2,... [WHERE 条件]

Multiple fields can be separated by commas. Each "field name = value" is to assign a value to the field, followed by the WHERE condition The statement can be used or not.

Note: The condition after set must use comma and not and. Multiple conditions after set are not related or related, so and cannot be used; where condition It can be followed by and.

Example:

update table set sex= '男', name='张三' where id = 1 ;          //正确
update table set sex= '男' and name='张三' where id = 1 ;    //错误

Method 2:

update 表名 set(字段1,字段2,字段3) = (select 数值1,数值2,数值3 from 表名 where....) where 条件

The above is the detailed content of How to update multiple fields at the same time using sql?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn