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.
Here is the example table structure, below image.
Group by MySql Query
SELECT * FROM `sample` GROUP BY fk_id
The result for the above Query is
Group by Last record
SELECT * FROM `sample` WHERE id IN ( SELECT MAX(id) FROM `sample` GROUP BY fk_id );
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.
tricks Jquery How to javascript php animation css MYSQL database browser
ReplyDelete