How do I change the following MySQL statement into a stored procedure or a stored function?

Source: Internet
Author: User
Tags acos cos
Here is an SQL statement that I used to calculate the distance from MySQL, which is too complex and I want to use MySQL stored procedures or storage functions to simplify


$select = 't.*,(ACOS(SIN((' . $this->latitude . '* 3.1415) / 180 ) *SIN((travel_user_status.latitude * 3.1415) / 180 ) +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 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

May I ask how I should do it??

Reply content:

Here is an SQL statement that I used to calculate the distance from MySQL, which is too complex and I want to use MySQL stored procedures or storage functions to simplify


$select = 't.*,(ACOS(SIN((' . $this->latitude . '* 3.1415) / 180 ) *SIN((travel_user_status.latitude * 3.1415) / 180 ) +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 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

May I ask how I should do it??

Self-Check manual AH: http://dev.mysql.com/doc/refman/5.0/en/create-function-udf.html

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.