Home >Backend Development >PHP Tutorial >如何将下面的mysql语句变成存储过程或者存储函数呢?

如何将下面的mysql语句变成存储过程或者存储函数呢?

WBOY
WBOYOriginal
2016-06-06 20:41:411063browse

下面是我使用mysql计算距离的一个sql语句,这个sql太复杂了,我想使用mysql 的存储过程或者存储函数来简化

<code><br> $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';
</code>

最后想变成这样:

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

这个distance就像是一个mysql函数可以直接调用

请问我该如何做呢??

回复内容:

下面是我使用mysql计算距离的一个sql语句,这个sql太复杂了,我想使用mysql 的存储过程或者存储函数来简化

<code><br> $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';
</code>

最后想变成这样:

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

这个distance就像是一个mysql函数可以直接调用

请问我该如何做呢??

自行查阅手册啊:http://dev.mysql.com/doc/refman/5.0/en/create-function-udf.html

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn