Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 866761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:51:46+00:00 2026-05-15T09:51:46+00:00

I have a table with details on personnel. I would like to create a

  • 0

I have a table with details on personnel. I would like to create a Next/Previous link based on the individual’s last name. Since personnel were not added in alphabetical order, selecting the next or previous row based on its ID does not work.

It is a hefty table – the pertinent fields are id, name_l, and name_f. I would like to order by name_l, the individuals’ last name.

How would I go about accomplishing this task?

Thanks!

Edit
This will be used on a Personnel Details page, the result will generate links to the next/prev entry in the database (ordered by last name) based on the current row. For example, if I am viewing Joe Hammer, the Next link would link to Frank Ingram.

Final code

Thanks to Daniel, here is what I finally got to work:

First, I set an increment at 0: $i = 0. Then, while looping through records with a while loop, I increased this by 1 = $i++. I then made a link to the details page for that particular entry:

<a href="details.php?id=<?php echo $member['id'];?>&amp;row=<?php echo $i;?>">Details</a>

On the Details page, I used the following SQL to select the next record:

$row = $_GET['row'];
$getNext = mysql_query("SELECT * FROM members ORDER BY name_l, id LIMIT ".$row.", 1");
$next = mysql_fetch_assoc($getNext);
$nextLink = $row + 1;

Finally, the link:

<a href="member_details.php?id=<?php echo $next['id'];?>&amp;row=<?php echo $nextLink;?>"><?php echo $next['name_l'] . ", " . $next['name_f'];?></a>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:51:47+00:00Added an answer on May 15, 2026 at 9:51 am

    First of all, make sure that your name_l column is indexed. Then you can simply use the ORDER BY and the LIMIT clauses as follows:

    SELECT * FROM personnel ORDER BY name_l, id LIMIT 0, 1;
    

    Simply increment the 0 value in the LIMIT clause to select the next record within the ordered set. Therefore use LIMIT 1, 1 to get the second record, LIMIT 2, 1 for the third, etc.

    To create an index on name_l you can use the CREATE INDEX command:

    CREATE INDEX ix_index_name ON personnel (name_l);
    

    Test case:

    CREATE TABLE personnel (
       id int not null primary key, 
       name_l varchar(10), 
       name_f varchar(10)
    );
    
    CREATE INDEX ix_last_name_index ON personnel (name_l);
    
    INSERT INTO personnel VALUES (1, 'Pacino', 'Al');
    INSERT INTO personnel VALUES (2, 'Nicholson', 'Jack');
    INSERT INTO personnel VALUES (3, 'De Niro', 'Robert');
    INSERT INTO personnel VALUES (4, 'Newman', 'Paul');
    INSERT INTO personnel VALUES (5, 'Duvall', 'Robert');
    

    Results:

    SELECT * FROM personnel ORDER BY name_l, id LIMIT 0, 1;
    +----+---------+--------+
    | id | name_l  | name_f |
    +----+---------+--------+
    |  3 | De Niro | Robert |
    +----+---------+--------+
    1 row in set (0.00 sec)
    
    SELECT * FROM personnel ORDER BY name_l, id LIMIT 1, 1;
    +----+--------+--------+
    | id | name_l | name_f |
    +----+--------+--------+
    |  5 | Duvall | Robert |
    +----+--------+--------+
    1 row in set (0.00 sec)
    
    SELECT * FROM personnel ORDER BY name_l, id LIMIT 2, 1;
    +----+--------+--------+
    | id | name_l | name_f |
    +----+--------+--------+
    |  4 | Newman | Paul   |
    +----+--------+--------+
    1 row in set (0.00 sec)
    

    1st UPDATE: (Further to the comments below)

    The above example is suitable mainly if you start by displaying the first record, and then you move sequentially to next, and maybe next again, and perhaps one back, etc. You could also easily move x steps forward and x steps backwards, but this does not appear to be required.

    If you start from a random record however, it will be more difficult to adapt the query above to get the next and previous records.

    If this is the case, then you can simply keep an index counter of where you currently stand. You start with $index = 0, and display the first record by using ... LIMIT $index, 1. Then you display the next by incrementing $index by 1, and you display the previous by decrementing $index by 1.

    If on the other hand, you will be rendering the personnel list, and then the user will click on one record (at random), you could also make this work with some help from the applications-side (php). Let’s say you render the following ordered list to the user:

    +----+-----------+--------+
    | id | name_l    | name_f |
    +----+-----------+--------+  // [Hidden info]
    |  3 | De Niro   | Robert |  // Row 0 
    |  5 | Duvall    | Robert |  // Row 1
    |  4 | Newman    | Paul   |  // Row 2
    |  2 | Nicholson | Jack   |  // Row 3
    |  1 | Pacino    | Al     |  // Row 4
    +----+-----------+--------+
    

    Now if the user clicks on Newman Paul, you would have to pass the row=2 parameter to the page that will display the details of this employee. The page that renders the details of the employee now knows that Newman Paul is the 3rd row (row=2). Therefore to get the previous and the next records you would simply change the x in LIMIT x, 1 by row - 1 for the previous record and row + 1 for the next:

    -- Previous 
    SELECT * FROM personnel ORDER BY name_l, id LIMIT 1, 1;
    +----+--------+--------+
    | id | name_l | name_f |
    +----+--------+--------+
    |  5 | Duvall | Robert |
    +----+--------+--------+
    1 row in set (0.00 sec)
    
    
    -- Next 
    SELECT * FROM personnel ORDER BY name_l, id LIMIT 3, 1;
    +----+-----------+--------+
    | id | name_l    | name_f |
    +----+-----------+--------+
    |  2 | Nicholson | Jack   |
    +----+-----------+--------+
    1 row in set (0.00 sec)
    

    2nd UPDATE:

    You can use the following MySQL-specific query to get the record number within the ordered list of any random employee, which then can be used to get the previous and next records. Note however that this is not very efficient, and may degrade performance if you have thousands of records.

    Let’s say you are in employee Nicholson Jack.

    You could do the following query:

    SELECT p.id, p.name_l, p.name_f, o.record_number
    FROM   personnel p
    JOIN   (
            SELECT    id,
                      @row := @row + 1 AS record_number
            FROM      personnel
            JOIN      (SELECT @row := -1) r
            ORDER BY  name_l, id
           ) o ON (o.id = p.id)
    WHERE  p.name_l = 'Nicholson' AND p.name_f = 'Jack';
    

    Which returns this:

    +----+-----------+--------+---------------+
    | id | name_l    | name_f | record_number |
    +----+-----------+--------+---------------+
    |  2 | Nicholson | Jack   |             3 |
    +----+-----------+--------+---------------+
    1 row in set (0.00 sec)
    

    Note that in the WHERE clause you could have used p.id = 2 if the id is known, instead of p.name_l = 'Nicholson' AND p.name_f = 'Jack'.

    Now we can use the record_number field, which is 3 in this case, to get the previous and the next records, simply by using the original query from the top of this answer, and replacing LIMIT 2, 1 for the previous and LIMIT 4, 1 for the next. There you go:

    The previous of Nicholson Jack:

    SELECT * FROM personnel ORDER BY name_l, id LIMIT 2, 1;
    +----+--------+--------+
    | id | name_l | name_f |
    +----+--------+--------+
    |  4 | Newman | Paul   |
    +----+--------+--------+
    1 row in set (0.00 sec)
    

    The next from Nicholson Jack:

    SELECT * FROM personnel ORDER BY name_l, id LIMIT 4, 1;
    +----+--------+--------+
    | id | name_l | name_f |
    +----+--------+--------+
    |  1 | Pacino | Al     |
    +----+--------+--------+
    1 row in set (0.00 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a table which consists of student details like roll no, Name, age.
I have the following example code: create table Details( name varchar(20), age int, weight
I have a table containing order details. I would like to be able to
I have table with 50 entries (users with such details like Name Surname Location
I have a table of contact details (name, province, etc) and I have a
I currently have a table structure that looks something like this(some details omitted): ColumnName
I have two entities // All details has been removed @Entity @Table(name = "A_TABLE_NAME")
I have table named Employee with id,first name, middle name, last name as fields.
I have table with following details Table name EMPLOYEE and columns EMPID (PK smallint
I have a table called Phone_Details and the data looks like: Name DeviceType InvoiceDate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.