
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Update two columns with a single MySQL query
For this, you need to use SET command only once. Let us first create a table −
mysql> create table DemoTable1909 ( Id int NOT NULL, FirstName varchar(20), LastName varchar(20) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1909 values(101,'John','Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1909 values(102,'John','Doe'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1909 values(103,'Adam','Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1909 values(104,'David','Miller'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1909;
This will produce the following output −
+-----+-----------+----------+ | Id | FirstName | LastName | +-----+-----------+----------+ | 101 | John | Smith | | 102 | John | Doe | | 103 | Adam | Smith | | 104 | David | Miller | +-----+-----------+----------+ 4 rows in set (0.00 sec)
Here is the query to update two column values −
mysql> update DemoTable1909 set FirstName='Carol',LastName='Taylor' where Id=103; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
Let us check the table records once again −
mysql> select * from DemoTable1909;
This will produce the following output −
+-----+-----------+----------+ | Id | FirstName | LastName | +-----+-----------+----------+ | 101 | John | Smith | | 102 | John | Doe | | 103 | Carol | Taylor | | 104 | David | Miller | +-----+-----------+----------+ 4 rows in set (0.00 sec)
- Related Questions & Answers
- Count two different columns in a single query in MySQL?
- MySQL query to combine two columns in a single column?
- How to bulk update MySQL data with a single query?
- Update multiple columns of a single row MySQL?
- Order by a function of two columns in a single MySQL query
- MySQL update multiple records in a single query?
- MySQL SELECT from two tables with a single query
- Comparing two columns in a single MySQL query to get one row?
- Change multiple columns in a single MySQL query?
- How can we update columns values on multiple rows with a single MySQL UPDATE statement?
- MySQL query to sort multiple columns together in a single query
- How to update two columns in a MySQL database?
- MySQL select and insert in two tables with a single query
- Can we update a row with the highest ID in a single MySQL query?
- How to sort multiple columns with a single query?
Advertisements