mySQL - update multiple columns with a select returning multiple rows

前端 未结 4 1562
臣服心动
臣服心动 2021-01-04 03:02

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  nearestPostco         


        
4条回答
  •  渐次进展
    2021-01-04 03:25

    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  ASC 
                            Limit 3
                            ) As Z1
                    ) As Z
    Set nearestPostCode1 = Z.PostCode1
        , nearestPostCode2 = Z.PostCode2
        , nearestPostCode3 = Z.PostCode3
    Where Table1.postcode =  'KY6 IDA'
    

提交回复
热议问题