
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL UPDATE query where id is highest AND field is equal to variable?
The syntax is as follows
update yourTableName set yourColumnName1=yourValue where yourColumnName2=yourValue order by yourIdColumnName DESC LIMIT 1;
To understand the above syntax, let us create a table. The query to create a table is as follows
mysql> create table UpdateWithHighestDemo -> ( -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> UserStatus tinyint, -> UserRank int -> ); Query OK, 0 rows affected (0.61 sec)
Insert some records in the table using insert command.
The query is as follows
mysql> insert into UpdateWithHighestDemo(UserStatus,UserRank) values(1,78); Query OK, 1 row affected (0.12 sec) mysql> insert into UpdateWithHighestDemo(UserStatus,UserRank) values(0,118); Query OK, 1 row affected (0.18 sec) mysql> insert into UpdateWithHighestDemo(UserStatus,UserRank) values(1,223); Query OK, 1 row affected (0.62 sec) mysql> insert into UpdateWithHighestDemo(UserStatus,UserRank) values(1,225); Query OK, 1 row affected (0.12 sec) mysql> insert into UpdateWithHighestDemo(UserStatus,UserRank) values(0,227); Query OK, 1 row affected (0.14 sec) mysql> insert into UpdateWithHighestDemo(UserStatus,UserRank) values(0,230); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from UpdateWithHighestDemo;
The following is the output
+--------+------------+----------+ | UserId | UserStatus | UserRank | +--------+------------+----------+ | 1 | 1 | 78 | | 2 | 0 | 118 | | 3 | 1 | 223 | | 4 | 1 | 225 | | 5 | 0 | 227 | | 6 | 0 | 230 | +--------+------------+----------+ 6 rows in set (0.00 sec)
Here is the query to update column
mysql> update UpdateWithHighestDemo -> set UserStatus=1 where UserRank=230 order by UserId DESC LIMIT 1; Query OK, 1 row affected (0.19 sec) Rows matched: 1 Changed: 1 Warnings: 0
Let us check and display records from the table using select statement.
The query is as follows
mysql> select *from UpdateWithHighestDemo;
The following is the output
+--------+------------+----------+ | UserId | UserStatus | UserRank | +--------+------------+----------+ | 1 | 1 | 78 | | 2 | 0 | 118 | | 3 | 1 | 223 | | 4 | 1 | 225 | | 5 | 0 | 227 | | 6 | 1 | 230 | +--------+------------+----------+ 6 rows in set (0.00 sec)
Now if you want to update with highest id then ORDER BY clause is useful. In the above sample output the highest ‘UserId’=6 and UserStatus is 1.
Let us update UserStatus to 0.
The query is as follows
mysql> update UpdateWithHighestDemo -> set UserStatus=0 order by UserId DESC LIMIT 1; Query OK, 1 row affected (0.18 sec) Rows matched: 1 Changed: 1 Warnings: 0
Check the records from the table using select statement.
The query is as follows
mysql> select *from UpdateWithHighestDemo; +--------+------------+----------+ | UserId | UserStatus | UserRank | +--------+------------+----------+ | 1 | 1 | 78 | | 2 | 0 | 118 | | 3 | 1 | 223 | | 4 | 1 | 225 | | 5 | 0 | 227 | | 6 | 0 | 230 | +--------+------------+----------+ 6 rows in set (0.00 sec)
- Related Articles
- Can we update a row with the highest ID in a single MySQL query?
- MySQL query to perform delete operation where id is the biggest?
- MongoDB query to exclude if id is equal to a document field array value
- MySQL query to find all rows where ID is divisible by 4?
- MySQL query to merge rows if Id is the same and display the highest corresponding value from other columns
- MySQL query to update string field by concatenating to it?
- How to write a valid MySQL query and update with a custom variable?
- Is there an operator in MySQL to implement multiple NOT conditions like WHERE id != 5 AND id != 10 AND id != 15?
- MySQL query to update only a single field in place of NULL
- MongoDB query to update array with another field?
- Only display row with highest ID in MySQL
- Is it possible to use UPDATE query with LIMIT in MySQL?
- MySQL update query to remove spaces?
- MongoDB query to update field and modify the data currently in column
- MongoDB query to find “where” Billing Address is Equal to Delivery Address from documents?

Advertisements