Skip to content
Advertisement

Compare rows in one table with MySQL

I tried to compare rows in one table that I have. The table is like this:

table

I have 3 rows for comparing, and they had tp 1,2,3 for each one with the same fid and different id. The question is, how can I compare three of them to return?

For example,

more explanation

For example, if ct of the tp 1 is equal to 0 so we return it, and tp 2 should compare with tp 1 if ct of the tp 1 is larger than 0 so we can return tp 2. if ct of the tp 2 and tp 1 is larger than 0 return tp 3. (all of them have the same fid)

the result should be like this:

I can handle this part without SQL. I can return all rows and compare them and return the result that I want, but It’s not a good solution, Because I want to handle all this part just with MySQL.

Advertisement

Answer

You seem to want the first time that ct = 0 for each fid based on the id. That suggests:

EDIT:

In older versions of MySQL, you can use:

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