SELECT last entry without using LIMIT in MySQL?



For this, you can use subquery. Let us first create a table −

mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(100)    -> ); Query OK, 0 rows affected (0.54 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(Name) values('John Smith'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(Name) values('David Miller'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Name) values('Chris Brown'); Query OK, 1 row affected (0.22 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

+----+--------------+ | Id | Name         | +----+--------------+ | 1  | John Smith   | | 2  | David Miller | | 3  | Chris Brown | +----+--------------+ 3 rows in set (0.00 sec)

Here is the query to get the last entry without using LIMIT in MySQL −

mysql> select *from DemoTable -> where Id=(select max(Id) from DemoTable);

Output

+----+-------------+ | Id | Name | +----+-------------+ | 3 | Chris Brown | +----+-------------+ 1 row in set (0.00 sec)
Updated on: 2019-07-30T22:30:26+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements