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 3391936
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:51:41+00:00 2026-05-18T03:51:41+00:00

I have a MySQL table with an auto-increment field and I am trying to

  • 0

I have a MySQL table with an auto-increment field and I am trying to figure out how to get the next or previous value along with the rest of that row using purely MySQL. The field is AI but the numbers aren’t necessarily consecutive as rows get deleted all the time.

field_id
8, 15, 17

This is what I started with:

//to get next value in table:
SELECT MIN(member_id) AS val FROM members WHERE member_id > 15 LIMIT 1

//to get previous value in table:
SELECT MAX(member_id) AS val FROM members WHERE member_id < 15 LIMIT 1

But that was only returning the member_id value. This works to get the next value (but doesn’t make a difference if I use DESC or ASC (wtf):

SELECT MIN(member_id),members.* 
AS val FROM members 
WHERE member_id > 15 
ORDER BY member_id DESC 
LIMIT 1

That query reversed (to get the the previous value) always returns the lowest value in the table (1):

SELECT MAX(member_id),members.* 
FROM members 
WHERE member_id < 15 
ORDER BY member_id ASC 
LIMIT 1

These two queries, however, shows what I want but I actually am not entirely sure why:

//get next LOWEST row
SELECT MAX(member_id),members.* 
FROM members 
WHERE member_id < 15 
GROUP BY member_id    
ORDER BY member_id DESC 
LIMIT 1

//get next HIGHEST row:
SELECT MIN(member_id),members.* 
FROM members 
WHERE member_id > 15 
GROUP BY member_id    
ORDER BY member_id ASC 
LIMIT 1

I’m assuming GROUP BY allows me to pull more than one row from the query? Is there a better way to do this?

  • 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-18T03:51:42+00:00Added an answer on May 18, 2026 at 3:51 am
    //to get next value in table:
    SELECT * FROM members WHERE member_id > 15 ORDER BY member_id LIMIT 1
    
    //to get previous value in table:
    SELECT * FROM members WHERE member_id < 15 ORDER BY member_id DESC LIMIT 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have a field in mySql table called jobnumber which auto increments with
In MySql I could have a table with an auto increment column and insert
I have a MySQL table, with the field ('ad_id'). I store variables in different
I have MySQL table with the following structure: +---------+--------------+------+-----+---------+----------------+ | Field | Type |
I have realised that mnesia doesnot support auto-increment feature as does MySQL or other
I have MySQL table that doesn't have AUTO_INCREMENT field. In PHP I run this
I have a MySQL table with a primary key field that has AUTO_INCREMENT on.
I have a MYSQL Table with the following structure called daily_measurements +------------+----------+------+-----+---------------------+----------------+ | Field
I have a MySQL table with the following schema: +--------------+-------------+------+-----+---------+----------------+ | Field | Type
I'm trying to create a Temp Table that has a key field that auto

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.