Skip to content
Advertisement

How to display the total count of the last row by filestatus?

I have to display the total count of the last row by filestatus.

tbl_bankdata

tbl_fileStatus

I have two tables tbl_bankdata and tbl_fileStatus. I am sending bank_id in the tbl_fileStatus as a f_bank_id.

Now I have to show the last f_bankid count.

For example, I have to fetch the count where f_filestatus=1. so my output will be 0. Why 0 because f_bankid 1 and 2 have a f_filestatus=1 but f_bankid 1 and 2 have the last row with f_filestatus the 3 and 2.

If I have to count f_filestatus=2 then I will get the output 1 and if count f_filestatus=3 then the output will be 2. why 2 because f_bank_id 1 have f_filestatus 3 and f_bank_id 3 have f_filestatus 3

This is my query

Would you help me out with this issue?

After suggested by @forpas

Advertisement

Answer

Use a correlated subquery:

Replace ? with the f_bankid you search for.
See the demo.

In MySql 8.0+ you can use FIRST_VALUE() window function:

See the demo.

If you want results for all f_filestatus in 1 row:

See the demo.
Results:

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