MySQL recursive query to find shortest path

I have a problem, I just can't hang my head ...

I have a table called country_neighbour

that looks like this.

Country_name Country_id Neighbour_name Neighbour_id
Italy              1      France              2
Italy              1      Switzerland         6
Italy              1      Austria             5
France             2      Spain               3
France             2      Italy               1
France             2      Switzerland         6
Spain              3      France              2
Spain              3      Portugal            4
Portugal           4      Spain               3 

      

What I want to get is the shortest way to go from one country to another, let's say that I want to know how many borders have to be crossed to get to Portugal from Italy.

(Italy β†’ France) 1

(Italy β†’ France β†’ Spain β†’ Portugal) 3

I searched for ideas and found WITH cte a good approach to my problem, but did not support MySQL

Is there anyone who can point me in the right direction. I appreciate all the help I can get. Thank.

+3


source to share





All Articles