get last id form group by with some specific condition

Viewed 23

database table record

I want the last record based on role and status For example, i want to create the rejected listing page for role = HO2 user you can see 2 entry in table for HO2 & rejected combination.I want the latest entry from database table. expected output: 27811->Ho2->Rejected Thanks in advance

1 Answers

Try this:

select * from table_name where id=(SELECT max(id) from table_name where status = 'rejected' and role = 'HO2');

But notice, this only works if your id column is auto increment. Otherwise you have to add a TIMESTAMP column and fill the value for each row, then get the latest row by the time.

Related