
- 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
MySQL: update field with Group By?
To update field with GROUP BY, use ORDER BY LIMIT with UPDATE command −
mysql> create table DemoTable2018 -> ( -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> EmployeeName varchar(20), -> EmployeeSalary int -> ); Query OK, 0 rows affected (0.61 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2018(EmployeeName,EmployeeSalary) values('Chris',10000); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable2018(EmployeeName,EmployeeSalary) values('David',12560); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable2018(EmployeeName,EmployeeSalary) values('Chris',25400); Query OK, 1 row affected (0.09 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable2018;
This will produce the following output −
+------------+--------------+----------------+ | EmployeeId | EmployeeName | EmployeeSalary | +------------+--------------+----------------+ | 1 | Chris | 10000 | | 2 | David | 12560 | | 3 | Chris | 25400 | +------------+--------------+----------------+ 3 rows in set (0.00 sec)
Here is the query to MySQL update field −
mysql> update DemoTable2018 -> set EmployeeSalary=EmployeeSalary+2500 where EmployeeName='Chris' -> order by EmployeeId limit 1; Query OK, 1 row affected (0.13 sec) Rows matched: 1 Changed: 1 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable2018;
This will produce the following output −
+------------+--------------+----------------+ | EmployeeId | EmployeeName | EmployeeSalary | +------------+--------------+----------------+ | 1 | Chris | 12500 | | 2 | David | 12560 | | 3 | Mike | 25400 | +------------+--------------+----------------+ 3 rows in set (0.00 sec)
- Related Questions & Answers
- How to ORDER BY FIELD with GROUP BY in a single MySQL query?
- Listing all rows by group with MySQL GROUP BY?
- HAVING with GROUP BY in MySQL
- MySQL query to update string field by concatenating to it?
- Get rows with GROUP BY in MySQL?
- How to update date of datetime field with MySQL?
- MySQL ORDER BY with custom field value
- Find values group by another field in MongoDB?
- GROUP BY the number of rows returned by GROUP BY in MySQL?
- Update only the int in MySQL Field
- MySQL query to group rows by the numeric characters in a string field?
- MySQL IF/WHEN/ELSE/OR with ORDER BY FIELD
- MySQL- GROUP and COUNT by date?
- Only update the MySQL field if the field contains null or 0?
- MongoDB query to update array with another field?
Advertisements