Home > Database > Mysql Tutorial > body text

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

青灯夜游
Release: 2019-05-15 15:23:03
Original
32047 people have browsed it

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 条件]
Copy after login

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 ;    //错误
Copy after login

Method 2:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template