
- 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
How to get the data associated with the maximum id in a MySQL table?
We will first order by DESC and then fetch the value associated with maximum id −
select *from yourTableName order by yourColumnName DESC LIMIT 1,1;
Let us first create a table −
mysql> create table DemoTable -> ( -> Alldata int -> ); Query OK, 0 rows affected (0.63 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(303); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(560); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(490); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(498); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+---------+ | Alldata | +---------+ | 303 | | 560 | | 100 | | 490 | | 498 | +---------+ 5 rows in set (0.00 sec)
Following is the query to get the record associated with the maximum id −
mysql> select *from DemoTable order by Alldata DESC LIMIT 1,1;
Output
+---------+ | Alldata | +---------+ | 498 | +---------+ 1 row in set (0.00 sec)
- Related Articles
- MySql how to display the records with latest ID in a table?
- How to get max(id) of row data in MySQL?
- How we can find all the triggers associated with a particular MySQL table?
- How to get the cookies associated with a document in JavaScript?
- How to get username using ID from another table in MySQL database?
- Fetch maximum ID value from the first table and insert to all the IDs in another table with MySQL INSERT INTO select?
- Get rows that have common value from the same table with different id in MySQL
- MySQL query to get count of each fileid entry in a table with Id and FileIDs?
- How to get the next auto-increment id in MySQL?
- How to select all the records except a row with certain id from a MySQL table?
- How to get the maximum value from strings with integers in MySQL?
- How to get the details of all the triggers associated with a job from AWS Glue Data catalog using Boto3
- How to get the creation date of a MySQL table?
- How to get the maximum value from a column with alphanumeric strings beginning with specific characters in MYSQL?
- Get the number of rows in a particular table with MySQL

Advertisements