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

  • SEARCH
  • Home
  • 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 6914641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:22:25+00:00 2026-05-27T09:22:25+00:00

I have a table, call it MyTable which has a column, say myvalue. I

  • 0

I have a table, call it MyTable which has a column, say “myvalue”.

I need a query which takes a number and returns a row based on some comparison with myvalue.

If number=myvalue then that is the correct row. If there is no match, then I need to take the value just above number. If that doesn’t exist I need to take the value just below number.

So for example let’s say the myvalue column has these values:

100
200
300
400
500

If I query with number=50 it will return the row with '100'.
If I query with number=100 it will return the row with '100'.  
If I query with number=101 it will return the row with '200'.
If I query with number=450 it will return the row with '500'.
If I query with number=570 it will return the row with '500'.

So far I have a query which will return either the matching value or the one right above it, but I don’t know how to make it return the one right below if the one above doesn’t exist?

This is what I do now:

SELECT * FROM MyTable WHERE myvalue >= 'number' ORDER BY myvalue ASC LIMIT 1

(of course replace ‘number’ with a value)

Any ideas? I can think of a brute force method but I’m sure there’s something elegant that I just can’t think of.

  • 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-27T09:22:25+00:00Added an answer on May 27, 2026 at 9:22 am
    SELECT *
    FROM (
          (
            SELECT *
            FROM MyTable
            WHERE myvalue >= 'number'
            ORDER BY myvalue ASC
            LIMIT 1
          )
          UNION 
          (
            SELECT *
            FROM MyTable
            WHERE myvalue <= 'number'
            ORDER BY myvalue DESC
            LIMIT 1
          )
        ) AS temp
    ORDER BY myvalue DESC 
    LIMIT 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Have an ASP:Table that has a row with cells in I need to
I have a table, let's call it My_Table that has a Created datetime column
I have a table, let's call is [MYTABLE] , with an FOR INSERT, UPDATE
I have a simple table (lets call it Table1) that has a NVARCHAR field
I have a table, call it TBL. It has two columns,call them A and
What I have? I have a simple web part which has a Table .
I have a simple database table (SQL Server 2008 R2 Express), which has a
I have a database table named call with columns call_time, location, emergency_type and there
I have problems filling a temporary table with the resultset from a procedure call
i have a table which looks similar to this <asp:TableRow><asp:TableCell>Question 1</asp:TableCell><asp:TableCell ID =Question1Text></asp:TableCell></asp:TableRow> <asp:TableRow><asp:TableCell

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.