How to get the last record in each MySql group by method | 2my4edge

15 March 2017

How to get the last record in each MySql group by method

How to get the last record of each MySql group by method, usually in group by method we will get first record data of the table, but in some cases you need last record in group by method. that time we need to use this mysql clause. this is also like group by category id or foreign id last record fetching method. Let see the MySql Query.

get last record in mysql


Here is the example table structure, below image.

get last record in mysql table structure

Group by MySql Query
SELECT * FROM `sample` GROUP BY fk_id



The result for the above Query is

get last record in mysql first result

Group by Last record
SELECT * FROM `sample` 
WHERE id 
     IN ( SELECT MAX(id) 
     FROM `sample` 
     GROUP BY fk_id );
The result for the above Query will be like

get last record in mysql last result
this will result the highest value of the id record will be displaying so that will result the last record in the group of result.



I hope this will help you in some cases, keep visiting our website for more solutions. thank you.

No comments:

Post a Comment

^