dragonball dragonball - 2 months ago 6
MySQL Question

How to count users ahead and behind of a particular user in PHP MYSQL

I have a pre-register form. After signup I have to show the current registerar his current position. The current position is determined by counting rows ahead and behind of particular row.

For example, if the signup ID of Jack Smith is 42 I want to show how many user are ahead of him registered and behind him. If the total record is supposed to have 200 enteries.

I am wondering how I can achieve this in MYSQL and PHP.

Thanks in advance!

Answer

You need to count the number of id lower than your current user's id and the number of id higher :

SELECT COUNT(ID) FROM my_table WHERE ID < 42;
SELECT COUNT(ID) FROM my_table WHERE ID > 42;

Hope this helps.

Comments