创建一个自定义的MySQL函数? [英] Creating a custom MySQL function?

查看:239
本文介绍了创建一个自定义的MySQL函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始在PHP中使用MySQL,我想知道是否可以创建自定义函数。这段代码应该说明我正在做什么。

  //一个不太适合嵌入到查询中的复杂公式
函数距离($ latA,$ lonA,$ latB,$ lonB)
{
//地球半径
$ radius = 3956;

$ latA = deg2rad($ latA);
$ lonA = deg2rad($ lonA);
$ latB = deg2rad($ latB);
$ lonB = deg2rad($ lonB);

//计算增量
$ deltaLat = $ latB - $ latA;
$ deltaLon = $ lonB - $ lonA;

//计算大圆距离
$ result = pow(sin($ deltaLatitude / 2.0),2)+(cos($ latA)* cos($ latB)* pow(sin ($ deltaLon / 2.0),2));
$ distance = $ radius * 2 * atan2(sqrt($ result),sqrt(1 - $ result));

返回$ distance;
}

//如何在我的查询中调用Distance()?
$ query =SELECT lat,lon FROM zipcodes WHERE Distance(lat,lon,0,0)< 20;
mysql_query($ query);

预先感谢您的帮助!

解决方案

您在应用程序中声明了这个MySQL函数,并且它将一直保留在数据库中,直到数据库服务器重新启动。



<$ p
RETURNS INT
READS SQL DATA
DETERMINISTIC $ b $ CREATE FUNCTION距离(LAT_A INT,LON_A INT,LAT_B INT,LON_B INT)$ p $ mysql_query b BEGIN
DECLARE半径,deltaLat,deltaLon,结果,距离BIGINT;
SET半径= 3956;
SET deltaLat = LAT_B-LAT_A;
SET deltaLon = LON_B-LON_A;
SET result = POW(SIN(deltaLat / 2),2)+(COS(LAT_A)* COS(LAT_B)* POW(SIN(deltaLon / 2.0),2));
SET distance = radius * 2 * ATAN2(SQRT(result),SQRT(1 - result));
RETURN distance;
END);

这使用MySQL的数学函数。将这个处理卸载到数据库是快速而有效的(数据不需要通过网络传输,只返回你想要的结果)。

一旦你声明了这个,你可以像这样使用它:

  $ query =SELECT lat,lon FROM zipcode where where Distance (lat,lon,0,0)<20; 
mysql_query($ query);

但是,如果数据库确实重新启动,则以前声明的任何函数或过程都会丢失。可以在应用程序级别处理MySQL错误1305(函数functionName不存在)。



在您的数据库错误处理程序:

  switch(mysql_errno()):
case 1305:
if(false = == $ database-> _declareStoredProcedureFlag){
if($ c = preg_match_all(/ FUNCTION [a-zA-Z0-9] + \。。
([a-zA- Z0-9 _] *)不存在/是,
mysql_error(),$ matches)
){
$ storedFunctionName = $ matches [1] [0];
$ database-> _declareStoredProcedureFlag = true;
if(true === $ database-> declareStoredFunction($ storedFunctionName)){
$ result = mysql_query($ query);
}
}
}
break;
...


I've just started using MySQL with PHP and I'd like to know if it's possible to create a custom function. This code snippet should illustrate what I'm trying to do.

// a somewhat complicated formula not suitable to embed into the query
function Distance($latA, $lonA, $latB, $lonB)
{
  // earth's radius
  $radius = 3956;

  $latA = deg2rad($latA);
  $lonA = deg2rad($lonA);
  $latB = deg2rad($latB);
  $lonB = deg2rad($lonB);

  // calculate deltas
  $deltaLat = $latB - $latA;
  $deltaLon = $lonB - $lonA;

  // calculate Great Circle distance
  $result = pow(sin($deltaLatitude / 2.0), 2) + (cos($latA) * cos($latB) * pow(sin($deltaLon / 2.0), 2));
  $distance = $radius * 2 * atan2(sqrt($result), sqrt(1 - $result));

  return $distance;
}

// how can I call Distance() in my query?
$query = "SELECT lat, lon FROM zipcodes WHERE Distance(lat, lon, 0, 0) < 20";
mysql_query($query);

Thanks in advance for any help!

解决方案

You an declare this MySQL function in your application, and it will remain in the database until the database server is restarted.

mysql_query("CREATE FUNCTION Distance(LAT_A INT, LON_A INT, LAT_B INT, LON_B INT, )
RETURNS INT
READS SQL DATA
DETERMINISTIC
BEGIN
DECLARE radius, deltaLat, deltaLon, result, distance BIGINT;
SET radius=3956;
SET deltaLat=LAT_B-LAT_A;
SET deltaLon=LON_B-LON_A;
SET result=POW(SIN(deltaLat/2), 2) + (COS(LAT_A) * COS(LAT_B) * POW(SIN(deltaLon/2.0), 2));
SET distance=radius * 2 * ATAN2(SQRT(result), SQRT(1 - result));
RETURN distance;
END");

This uses MySQL's mathematical functions. Offloading this processing to the database is fast and efficient (the data doesn't have to travel across the wire, and you're only returned the results you want).

Once you've declared this, you can use it like so:

$query = "SELECT lat, lon FROM zipcodes WHERE Distance(lat, lon, 0, 0) < 20";
mysql_query($query);

However if your database does restart, any functions or procedures declared previously are lost. It's possible to handle MySQL error 1305 (Function functionName does not exist) gracefully at the application level.

In your database error handler:

switch (mysql_errno()):
    case 1305:
        if (false === $database->_declareStoredProcedureFlag) {
             if ($c = preg_match_all("/FUNCTION [a-zA-Z0-9]+\." .
                 "([a-zA-Z0-9_]*) does not exist/is",
                 mysql_error(), $matches)
             ) {
                 $storedFunctionName = $matches[1][0];
                 $database->_declareStoredProcedureFlag = true;
                 if (true === $database->declareStoredFunction($storedFunctionName)) {
                     $result = mysql_query($query);
                 }
             }
         }
         break;
    ...

这篇关于创建一个自定义的MySQL函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆