

- 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
Can we update a row with the highest ID in a single MySQL query?
Yes, we can do that. Let us first create a table −
mysql> create table DemoTable ( ID int, GameScore int ); Query OK, 0 rows affected (0.55 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(15,848747); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(13,909049); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(34,98474646); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(31,948474); Query OK, 1 row affected (0.27 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+------+-----------+ | ID | GameScore | +------+-----------+ | 15 | 848747 | | 13 | 909049 | | 34 | 98474646 | | 31 | 948474 | +------+-----------+ 4 rows in set (0.00 sec)
Following is the query to update a row with the highest ID in a single query −
mysql> update DemoTable set GameScore=GameScore+10 ORDER BY ID DESC LIMIT 1; Query OK, 1 row affected (0.19 sec) Rows matched : 1 Changed : 1 Warnings : 0
Let us check the table record once again −
mysql> select *from DemoTable;
Output
+------+-----------+ | ID | GameScore | +------+-----------+ | 15 | 848747 | | 13 | 909049 | | 34 | 98474656 | | 31 | 948474 | +------+-----------+ 4 rows in set (0.00 sec)
- Related Questions & Answers
- How can I select the row with the highest ID in MySQL?
- MySQL query to get the highest value from a single row with multiple columns
- Only display row with highest ID in MySQL
- Update two columns with a single MySQL query
- How can we update columns values on multiple rows with a single MySQL UPDATE statement?
- How can we delete a single row from a MySQL table?
- Update multiple columns of a single row MySQL?
- MySQL update multiple records in a single query?
- MySQL UPDATE query where id is highest AND field is equal to variable?
- How to bulk update MySQL data with a single query?
- Write a single MySQL query to return the ID from the corresponding row which is a NOT NULL value
- Can we use WHERE, AND & OR in a single MySQL query?
- A single MySQL query to find the highest and lowest among two tables?
- MySQL query to return all items in a single row
- MongoDB query to update a MongoDB row with only the objectid
Advertisements