Skip to content
Advertisement

Select the last record in MySQL in WHERE condition

I have a table that store ticket and statue relation

I want to select rows that last status_Id equal 2 ,Rows are marked in table. I think I have to use GROUP BY on column ticket_Id but it return with first status_Id.

Advertisement

Answer

This problem is a good candidate for ROW_NUMBER:

The above logic finds all latest rows for each ticket_Id, as ordered by the ticketstatus_Id, whose status_Id values also happens to be 2.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement