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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:58:09+00:00 2026-06-10T00:58:09+00:00

Dilemma: I have a table with several hundred rows in it. I would like

  • 0

Dilemma: I have a table with several hundred rows in it. I would like to submit a mysql_query with a specific search term wherein the query finds where the term would be located alphabetically in the column and then returns the n rows before the placement and n rows after the placement.

Example: Imagine that I have a column like the following (I’m placing it horizontally for the sake of space, but for sake of argument, let’s pretend that this is a vertical list of column values):

|apple|asus|adder|billy|cat|dog|zebra|computer|mouse|cookie|donkey|

If I were to run the query on the term courage, assuming n = 3, I would like to have it return the following rows in this order:

|cat|computer|cookie|dog|donkey|mouse|

Alphabetically, the word courage would land right in the middle of those results and we are met with the preceding 3 entries and the following 3 entries.

Language Notes: I’m using php and mysql. I don’t have any code to display because I’m not sure whether this needs to be in the where clause, or if it requires a subquery, or if you need to do something with the variable in php before handing it to the query.

  • 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-06-10T00:58:10+00:00Added an answer on June 10, 2026 at 12:58 am

    You might try an UNION of two SELECTS. Or do it manually.

    SELECT term FROM table WHERE term < 'courage' ORDER BY term DESC LIMIT 3;
    

    will return cookie, computer and cat (in descending order).

    Then
    SELECT term FROM table WHERE term >= ‘courage’ ORDER BY term ASC LIMIT 3;

    will return dog, donkey and mouse.

    In PHP, you get the two sets, reverse the first and join.

    A wholly-SQL solution might be

    SELECT term FROM (
        SELECT term FROM table WHERE term < 'courage' ORDER BY term DESC LIMIT 3 )
        UNION
        SELECT term FROM table WHERE term >= 'courage' ORDER BY term ASC LIMIT 3 )
    ) ORDER BY term;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this dilemma many times - and I would like one time and
I have encountered the following dilemma several times and would be interested to hear
Here's the dilemma: I have a table with rows. I need to count based
I am having a dilemma. I have a table with a column called ID
I have a dilemma, maybe you can help me. I have a table which
Guys I'm stumped on this one, I have a large table with 100,000 rows
I have to do a project about the periodic table of elements. My dilemma
I've got an interesting dilemma now. I have a database schema like the following:
Here is my dilemma; I have on my page a table as such: <table
I have a dilemma. Let's assume(for simplicity's sake) I have four tables, with different

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.