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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:46:32+00:00 2026-05-22T01:46:32+00:00

I have a table of postcodes and I want to update each postcode with

  • 0

I have a table of postcodes and I want to update each postcode with its 3 nearest neighbours. Ie to fill in the blanks in this table:

postcode  nearestPostcode1  nearestPostcode2  nearestPostcode3
_______________________________________________________________

KY6 1DA      -                -                  -
KY6 1DG      -                -                  -
KY6 2DT      -                -                  -
KY6 1RG      -                -                  -
....

I’ve figured out a SELECT query to find the nearest postcodes and here is one clumsy way the first row could be updated:

update table1 set 
nearestPostcode1 = (select query for returning the first nearest postcode),
nearestPostcode2 = (select query for returning the second nearest postcode),
nearestPostcode3 = (select query for returning the third nearest postcode)
where postcode = 'KY6 1DA';

However this will result in 3 select queries being run for each row update. It would be more efficient if there was some way to do what is expressed by this pseudo code:

update table1 set 
(nearestPostcode1, nearestPostcode2, nearestPostcode3) = 
(select query to return the 3 nearest postcodes)
where postcode = 'KY6 1DA';

The ‘select query’ in the above looks like this:

select postcode from postcodeTable 
order by <equation to calculate distance> ASC 
limit 3

Is there anyway for the rows returned from the select to be put into a form that they can be used to update multiple fields?
Thanks.

  • 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-22T01:46:33+00:00Added an answer on May 22, 2026 at 1:46 am
    Update Table1
        Cross Join  (
                    Select Min( Case When Z1.Num = 1 Then Z1.postcode End ) As PostCode1
                        , Min( Case When Z1.Num = 2 Then Z1.postcode End ) As PostCode2
                        , Min( Case When Z1.Num = 3 Then Z1.postcode End ) As PostCode3
                    From    (
                            Select postcode 
                                , @num := @num + 1 As Num
                            From postcodeTable 
                            Where postcode = 'KY6 IDA'
                            Order By <equation to calculate distance> ASC 
                            Limit 3
                            ) As Z1
                    ) As Z
    Set nearestPostCode1 = Z.PostCode1
        , nearestPostCode2 = Z.PostCode2
        , nearestPostCode3 = Z.PostCode3
    Where Table1.postcode =  'KY6 IDA'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a contacts table which contains fields such as postcode , first name
Imagine I have table like this: id:Product:shop_id 1:Basketball:41 2:Football:41 3:Rocket:45 4:Car:86 5:Plane:86 Now, this
I have a table with more than a millon rows. This table is used
I have a table of contacts and a table of postcode data. I need
I have a table of names and addresses, which includes a postcode column. I
I have two tables, landlords and properties. My properties table has; ID, Address, Postcode,
i have table question (mysql with php) which contains question,category(multiple) and subcat(multiple) its belong.
I have table like this create table tbl_1( year int, month int, day int
I have member table member_Id Last name first name postcode reference another table memberstomships
I have a table that records distance from Google/Bing between two valid UK postcodes.

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.