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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:25:54+00:00 2026-05-16T17:25:54+00:00

I have a MySQL table with about 1000 rows, filled with different types of

  • 0

I have a MySQL table with about 1000 rows, filled with different types of fruit trees and their locations on a farm. The data looks like this:

Fruit Trees Location on Farm

  7 |
  6 |    G G
  5 |    G   
Y 4 |    G   G
  3 |      A X G G
  2 |    A A A
  1 |_ _ _ _ _ _ _ 
     1 2 3 4 5 6 7
           X

Fruit Trees MySQL Table

 ID   Type    X  Y
 --   ----    -- --
 1    Apple   3  2
 2    Grape   3  4
 3    Grape   3  5
 4    Grape   3  6
 5    Apple   4  2
 6    Apple   4  3
 7    Grape   4  6
 8    Apple   5  2
 9    Grape   5  4
10    Grape   6  3
11    Grape   7  3

Now, I am looking to query this table to get the nearest “3” trees only of each type from the center X (5,3).

The query result should return something like:

 ID   Type    X  Y
 --   ----    -- --
 6    Apple   4  3
 5    Apple   4  2
 8    Apple   5  2
 9    Grape   5  4
10    Grape   6  3
11    Grape   7  3

There may be several trees with the same distance from the center but that should not matter because once the maximum number of nearest trees (3) is meet then we wouldn’t include anymore trees from that type of tree. Hope this makes sense.

To get the distance from the center X to a nearby tree I use the below formula:

Distance = SQRT( POW(ABS(5 - X),2) + POW(ABS(3 - Y),2) )

Here is a reference that I have been trying to use:
Sum until certain point – MySql

How would I write a MySQL query to get the results?
I may have to write several queries to do this but am just unsure how to structure it all.

Thank you

  • 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-16T17:25:55+00:00Added an answer on May 16, 2026 at 5:25 pm

    You can simulate row numbering with variables.

    SELECT @i:=0, @type:=''
    ;
    
    SELECT
          id, type, x, y
    FROM (
        SELECT
              id, type, x, y
            , SQRT( POW(ABS(5 - x),2) + POW(ABS(3 - y),2) ) AS distance
            , IF( type=@type, @i:=@i+1, @i:=0             ) AS rank
            , IF( type=@type, @type,    @type:=type       ) AS new_type
    
        FROM
            fruit_trees
        ORDER BY
              type
            , distance
        ) x
    WHERE
        rank < 3
    

    Seems to work for me, but I expect performance will be ropey.

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

Sidebar

Related Questions

I have a MySQL table with about 5,000,000 rows that are being constantly updated
I have a mysql DB table of about 10.000 rows and about 20 fields.
I currently have a MySQL table of about 20 million rows, and I need
I have a MySql table which has about 100k rows. there is one field
I have a large mysql table (about 5M rows) on which i frequently insert
I have a large MySQL table (about 750 million rows) and I just want
I have a MySQL table with about 2 million rows, and a script that
I have got a mysql table containing data about companies: id, company name, city,
I have a MySQL table about 1000 million records. It is very slow when
I have a mysql table with about 2,000,000 entries, with a primary key which

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.