drop function distance; delimiter // create function distance ( source char(12) , destination char(12) ) returns double begin if mid(source,1,1) <> mid(destination,1,1) then return 0; end if; SET @s_gal0=mid(source,1,1); SET @s_gal1=cast( (mid(source,(1)+1,1)) as unsigned int); SET @s_gal2=cast( (mid(source,(2)+1,1)) as unsigned int); SET @s_reg0=cast( (mid(source,(4)+1,1)) as unsigned int); SET @s_reg1=cast( (mid(source,(5)+1,1)) as unsigned int); SET @s_sys0=cast( (mid(source,(7)+1,1)) as unsigned int); SET @s_sys1=cast( (mid(source,(8)+1,1)) as unsigned int); SET @s_ast0=cast( (mid(source,(10)+1,1)) as unsigned int); SET @s_ast1=cast( (mid(source,(11)+1,1)) as unsigned int); SET @t_gal0=mid(destination,1,1); SET @t_gal1=cast( (mid(destination,(1)+1,1)) as unsigned int); SET @t_gal2=cast( (mid(destination,(2)+1,1)) as unsigned int); SET @t_reg0=cast( (mid(destination,(4)+1,1)) as unsigned int); SET @t_reg1=cast( (mid(destination,(5)+1,1)) as unsigned int); SET @t_sys0=cast( (mid(destination,(7)+1,1)) as unsigned int); SET @t_sys1=cast( (mid(destination,(8)+1,1)) as unsigned int); SET @t_ast0=cast( (mid(destination,(10)+1,1)) as unsigned int); SET @t_ast1=cast( (mid(destination,(11)+1,1)) as unsigned int); SET @s_sys_x=@s_reg1*10+@s_sys1; SET @s_sys_y=@s_reg0*10+@s_sys0; SET @t_sys_x=@t_reg1*10+@t_sys1; SET @t_sys_y=@t_reg0*10+@t_sys0; SET @var_gal=ABS((@s_gal1-@t_gal1)*19+@s_gal2-@t_gal2); SET @var_sys=CEIL(SQRT(POW(@t_sys_x-@s_sys_x,2)+POW(@t_sys_y-@s_sys_y,2))); SET @var_ast0=ABS(@t_ast0-@s_ast0); SET @var_ast1=ABS(@t_ast1-@s_ast1); if (@var_gal) then if (@t_gal1=@s_gal1) then SET @distance=@var_gal*200; end if; if (@t_gal1>@s_gal1) then SET @distance=(9-@s_gal2)*200+2000+@t_gal2*200; end if; if (@t_gal1<@s_gal1) then SET @distance=@s_gal2*200+2000+(9-@t_gal2)*200; end if; else if (@var_sys) then SET @distance=@var_sys; else if (@var_ast0) then SET @distance=@var_ast0/5; else SET @distance=0.1; end if; end if; end if; return @distance; end; // delimiter ; select distance ( 'D13:63:93:21' , 'D13:63:93:12' ) , 0.2 ; select distance ( 'D13:63:93:21' , 'D13:63:52:22' ) , 5 ; select distance ( 'D13:63:93:21' , 'D13:43:93:21' ) , 20 ; select distance ( 'D13:63:93:21' , 'D12:43:93:21' ) , 200 ; select distance ( 'D13:63:93:21' , 'D32:43:93:21' ) , 3600 ;