update multiple columns in mysql

How To Update Multiple Columns in MySQL

Sometimes you may need to update multiple columns in MySQL. In this article we will look at how to update multiple columns in MySQL with single query.


How To Update Multiple Columns in MySQL

Here are the steps to update multiple columns in MySQL.

UPDATE statement allows you to update one or more values in MySQL. Here is the syntax to update multiple values at once using UPDATE statement.

UPDATE [LOW_PRIORITY] [IGNORE] table_name
SET
column_name1 = expr1,
column_name2 = expr2,
…
[WHERE condition];

In the above statement, you need to specify the table_name, then mention the columns you want to update, along with their new values, one after the other, separated by commas. Finally, you need to optionally specify a WHERE clause to filter the rows you want to update.

Also Read : How to Escape Single Quote, Special Characters in MySQL

MySQL Update Example

Let us say you have the following table employees(id, first_name, last_name)

mysql> create table employees(id int, first_name varchar(255),last_name varchar(255));

mysql> insert into employees(id, first_name, last_name)
       values(1,'John','Doe'),
       (2,'Jane','Doe');

mysql> select * from employees;
+------+------------+-----------+
| id   | first_name | last_name |
+------+------------+-----------+
|    1 | John       | Doe       |
|    2 | Jane       | Doe       |
+------+------------+-----------+

Here’s the SQL query to update multiple columns first_name and last_name in single query.

mysql> update employees
       set  first_name='Jim',
       last_name='Don'
       where id=1;

mysql> select * from employees;
+------+------------+-----------+
| id   | first_name | last_name |
+------+------------+-----------+
|    1 | Jim        | Don       |
|    2 | Jane       | Doe       |
+------+------------+-----------+

As you can see, both first_name and last_name columns have been updated at once.

Ubiq makes it easy to visualize data, and monitor them in real-time dashboards. Try Ubiq for free.

mm

About Ubiq

Ubiq is a powerful dashboard & reporting platform. Build dashboards, charts & reports for your business in minutes. Try it for free!