First and Last Record in Mysql - Mostlikers

12 August, 2014

First and Last Record in Mysql

Sometime we required last insert value just assign order wise your fetching query. below i write some useful way fetching database records.

Sql query

Last Record in database

This query show your table last insert value
SELECT * FROM table_name ORDER BY column_id DESC;


First Record

This query show your table First insert value

SELECT * FROM table_name ORDER BY column_id ASC;

Limit

This query show your table limited records
SELECT * FROM table_name LIMIT 5;

2nd value record

SELECT * FROM table_name LIMIT 2,1;


find the second largest value

SELECT MAX(colum)FROM table  WHERE col < ( SELECT MAX( colum )  FROM table );

Oreder by date


SELECT * FROM table_name  ORDER BY DATE(updated_at) DESC

No comments:

Post a Comment