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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:38:40+00:00 2026-06-18T05:38:40+00:00

I have a MySQL database which contains ranges of numbers. It has the following

  • 0

I have a MySQL database which contains ranges of numbers. It has the following structure:

range_id, start, end

All columns are INT(10). Furthermore there is a Polygon field called range_poly used for indexing.

I want to flag all the ‘outer’ ranges: all ranges that are not contained within another range in the database. For example:

range_id  |  start |  end
    1     |    1   | 2
    2     |    4   | 5
    3     |    1   | 10

In this case the third record is an ‘outer range’ because it is not contained within another range, but the first and second records are not because they are completely contained within record 3. In order to achieve this I added a column called is_outer which is a simple INT(1) to indicate whether or not the range is contained within another one. I am using the following php script:

$result = mysql_query(mysql_real_escape_string("SELECT range_id, start FROM table;"), $db);

while($row = mysql_fetch_array($result))
{
    $result2 = mysql_query(mysql_real_escape_string("SELECT range_id FROM table WHERE MBRCONTAINS( range_poly, POINTFROMWKB( POINT( ". $row['start'] ." , 0 ) ) ) ORDER BY (`end` - `start`) DESC LIMIT 1;"), $db); 
    $row2 = mysql_fetch_array($result2);
    mysql_query(mysql_real_escape_string("UPDATE table SET is_outer = 1 WHERE range_id = ". $row2['range_id'] . ";"), $db);
}

This works fine but I can’t help feeling that there should be an easier way to achieve this. I can’t seem to wrap my brain around a way to do this is pure SET based queries. Alternatively I can code this using a CURSOR but I wonder if the performance will be much better compared to the PHP version. My database has about 3.7M records which explains why performance is quite important.

I have tried to use a subquery but LIMIT is not allowed in a subquery. Alternatively I am thinking of joining the table on itself but I can’t wrap my head around the right conditions.

  • 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-18T05:38:41+00:00Added an answer on June 18, 2026 at 5:38 am

    If I’m understanding your question correctly, you want to return all the parents (outer ranges).

    If so, try something like this:

    SELECT 
      t1.range_id
    FROM YourTable t1
       LEFT JOIN YourTable t2 ON 
        t1.start >= t2.start AND t1.end <= t2.end AND t1.range_id <> t2.range_id
    WHERE t2.range_id IS NULL
    

    And here is the SQL Fiddle.

    If you have duplicated data (7, 33,36) and (8, 33,36), and you want both to return, you can add this to your WHERE clause:

    OR (t1.start = t2.start AND t1.end = t2.end)
    

    Good luck.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL database called People which contains the following schema <id,name,foodchoice1,foodchoice2> .
I have MySQL database, where I store the following BLOB (which contains JSON object)
I have a MySQL database which contains pages of my website. All of the
I have two tables in MYSQL database which contains same Columns by counting and
I have a PHP program w/MySQL database which contains many records. Two columns of
I have a table in a MySQL Database which has this structure: CREATE TABLE
I have this MySQl database that has a table which contains a column that
I have a MySQL database which contains data i would like to populate into
I have table which contains double values stored in mysql database ...I need to
i have a large mysql database table in which one column contains values ranging

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.