Is this in PHP? I would do something along the lines of a while loop. I'm not sure you will be able to accomplish what you want with one SQL statment, but if you can you will need to use the COUNT() function in SQL. If you get stuck let me know and I can help you out with some php/sql stuff to get you where you want to be.