在NULL值上加入MySQL

时间:2015-11-30 01:23:49

标签: mysql

我必须在MySQL中进行查询。我必须找到所有机场,最多可以在4个步骤内到达。 这是我的问题:

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

结果:

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

结果错误2点(我用星星标记)。问题是,我检查,“到”机场a1是否等于“从”机场a2。但是如果“到”机场是NULL,我得到错误的结果。

有人可以帮我解决问题吗? 谢谢

PS:我不允许以递归方式执行此操作。

1 个答案:

答案 0 :(得分:0)

如果要删除带NULL的行,请在查询末尾添加:

  

AND fp4.to IS NOT NULL