I have to do a query in MySQL. I have to find all airports, which can be reached within maximum 4 stepovers. This is my query:
SELECT fp0.from AS start, fp1.from AS stop1,
fp2.from AS stop2, fp3.from AS stop3,
fp4.from AS stop4,fp4.to AS end
FROM flightplan AS fp0
LEFT JOIN flightplan AS fp1 ON( fp0.to=fp1.from )
LEFT JOIN flightplan AS fp2 ON( fp1.to=fp2.from )
LEFT JOIN flightplan AS fp3 ON( fp2.to=fp3.from )
LEFT JOIN flightplan AS fp4 ON( fp3.to=fp4.from )
WHERE fp0.from=6626
The result:
6626,"9895","4887","12836","10304","9915"
6626,"9895","4887","12836","10304","4595"
6626,"9895","4887","12836","10304","2685"
6626,"9895","4887","12836","164","12081"
6626,"9895","4887","12836","1652","8686"
6626,"9895","4887","12836","1298","6682"
6626,"9895","4887","12836","5965","10953"
6626,"9895","4887","12836","5965","7212"
6626,"9895","13070",**NULL**,NULL,NULL
6626,"9895","13070","4859","12820","6908"
6626,"9895",**NULL**,NULL,NULL,NULL
The result is wrong at 2 points ( I marked them with stars ). The problem is, that I check, if the "to" airport a1 equals the "from" airport a2. But if the "to" airport is NULL, I get the wrong result.
Can somebody help me with my problem? Thank you
PS: I am not allowed to do this recursively.
If you want to eliminate the rows with NULL, add this at the end of your query:
AND fp4.to IS NOT NULL
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.