How to turn the following mysql statement into a stored procedure or stored function?

  mysql, question

The following is an sql statement that I use mysql to calculate distance. This sql is too complex. I want to simplify it by using mysql stored procedures or stored functions


$select = 't.*,(ACOS(SIN((' . $this->latitude . '* 3.1415) / 180 ) *SIN((travel_user_status.latitude * 3.1415) / 180 ) Plus cos (('.$ this-> latitude.' * 3.1415)/180) * COS((travel_user_status.latitude * 3.1415) / 180 ) * COS((' . $this->longitude . '* 3.1415) / 180 - (travel_user_status.longitude * 3.1415)/180 )) * 6378.137) as distance'; $join = 'LEFT JOIN travel_user_status ON travel_user_status.userId=t.userId';

Finally want to become like this:

$select = 't.*,distance($this->latitude,$this->longitude)  as distance';
 $join = 'LEFT JOIN travel_user_status ON travel_user_status.userId=t.userId';

This distance is like a mysql function that can be called directly

What should I do? ?