
- 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
Update only a single value from a MySQL table where select from same table ordered in descending order?
For this, use ORDER BY DESC with the LIMIT clause. The ORDER BY DESC order in descending order where LIMIT sets the number of records you want. Here, we will set LIMIT 1 since we want only a single record. Let us first create a table −
mysql> create table DemoTable ( StudentName varchar(100), StudentMarks int ); Query OK, 0 rows affected (0.54 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Chris',45); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Bob',78); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Mike',34); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Robert',67); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------------+--------------+ | StudentName | StudentMarks | +-------------+--------------+ | Chris | 45 | | Bob | 78 | | Mike | 34 | | Robert | 67 | +-------------+--------------+ 4 rows in set (0.00 sec)
Following is the query to update only a single value from a MySQL table ordered in descending order −
mysql> update DemoTable set StudentName='Adam' order by StudentMarks DESC LIMIT 1; Query OK, 1 row affected (0.10 sec) Rows matched: 1 Changed: 1 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+-------------+--------------+ | StudentName | StudentMarks | +-------------+--------------+ | Chris | 45 | | Adam | 78 | | Mike | 34 | | Robert | 67 | +-------------+--------------+ 4 rows in set (0.00 sec)
- Related Articles
- Select from table where value does not exist with MySQL?
- How to select only 3 ordered rows on a MySQL table?
- MySQL select only a single value from 5 similar values?
- A single MySQL query to select value from first table and insert in the second?
- Selecting a value in custom order from another column in a MySQL table with a single query
- How to fetch only a single result from a table in Java-MySQL?
- How do I select data from one table only where column values from that table match the column values of another table in MySQL?
- How to select data from a table where the table name has blank spaces in MYSQL?
- Update only a single column value in MySQL
- Insert values in a table by MySQL SELECT from another table in MySQL?
- How can I update a field in a MySQL database table by adding a value in the second table with a value from the first table?
- How to delete a single value from a MySQL table with duplicate records?
- Select words from a text already in a MySQL table
- MySQL select query to select rows from a table that are not in another table?
- Select some data from a database table and insert into another table in the same database with MySQL

Advertisements