MySQL Query & PHP for a list of data, best way to go about it?

I’m somewhat confused how to go about getting the MySQL right for something I want to do. Also I’m not sure what’s the best way to do it.

WHAT I’M TRYING TO DO

I have a database with users who have various ranks, I want to take… say 50 newest users and display them separated by their rank, not only that I also want to sort of split it into two columns (not halfway, after a rank list is done). But I also want to put a limit of max 15 users per rank.

I could probably figure out the php part, but will love suggestions.

TABLE users


    `order` (int), `user` (varchar), `rank` (varchar), `url` (text), `joindate` (timestanp)


DESIRED/SAMPLE MARKUP

<div id="container">
        <div class="left">
            <h2>NOVICE</h2>
    
            <ul class="userlist">
                <li><a href="THE_URL">USERNAME 1</a></li>
                <li><a href="THE_URL">USERNAME 2</a></li>
                <li><a href="THE_URL">USERNAME 3</a></li>
                <li><a href="THE_URL">USERNAME 4</a></li>
                <li><a href="THE_URL">USERNAME 5</a></li>
                <li><a href="THE_URL">USERNAME 6</a></li>
                <li><a href="THE_URL">USERNAME 7</a></li>
                <li><a href="THE_URL">USERNAME 8</a></li>
            </ul>
    
            <h2>INTERMEDIATE</h2>
    
            <ul class="userlist">
                <li><a href="THE_URL">USERNAME 9</a></li>
                <li><a href="THE_URL">USERNAME 10</a></li>
                <li><a href="THE_URL">USERNAME 11</a></li>
                <li><a href="THE_URL">USERNAME 12</a></li>
            </ul>
        </div>
    
        <div class="right">
            <h2>SKILLED</h2>
    
            <ul class="userlist">
                <li><a href="THE_URL">USERNAME 13</a></li>
                <li><a href="THE_URL">USERNAME 14</a></li>
                <li><a href="THE_URL">USERNAME 15</a></li>
            </ul>
    
            <h2>EXPERT</h2>
    
            <ul class="userlist">
                <li><a href="THE_URL">USERNAME 16</a></li>
                <li><a href="THE_URL">USERNAME 17</a></li>
                <li><a href="THE_URL">USERNAME 18</a></li>
                <li><a href="THE_URL">USERNAME 19</a></li>
                <li><a href="THE_URL">USERNAME 20</a></li>
                <li><a href="THE_URL">USERNAME 21</a></li>
                <li><a href="THE_URL">USERNAME 22</a></li>
            </ul>
        </div>
    </div>

MAIN QUESTION

What is the best way to take top 50 new entries, group them into the rank (should only take max 15 users with the same rank), and return the SQL result? Can be done with single MySQL query? or no way to do it other than using multiple queries?

how many different ranks are there?

i’m thinking separate queries for each, with LIMIT 15

There will be “custom ranks” sort of, even though there is 4 now, it can increase.

There is absolutely no way to other than separate queries? I’m trying to keep number of queries as small as possible. I guess I’ll have to work with what’s possible. :slight_smile:

if you can re-think the “top 50 with 15 each” idea, i can re-think separate queries

If SQL Consultant such as yourself suggest me to go for multiple queries, then thats exactly what I’ll do. Thanks! :slight_smile: