我回答自己的问题
似乎我没有权限删除MySQL函数…
所以我换了
DROP FUNCTION IF EXISTS `ST_Distance_Sphere`
通过mysql版本检查:
$mysql_version_check = DB::select(DB::raw('SHOW VARIABLES LIKE "version";'));
$mysql_version = $mysql_version_check[0]->Value;
if (substr($mysql_version,2, 1) < '7' AND substr($mysql_version,4, 1) < '6') {
$sql = '
CREATE FUNCTION `ST_Distance_Sphere` (point1 POINT, point2 POINT)
RETURNS FLOAT
no sql deterministic
BEGIN
declare R INTEGER DEFAULT 6371000;
declare `Ï1` float;
declare `Ï2` float;
declare `ÎÏ` float;
declare `Îλ` float;
declare a float;
declare c float;
set `Ï1` = radians(y(point1));
set `Ï2` = radians(y(point2));
set `ÎÏ` = radians(y(point2) - y(point1));
set `Îλ` = radians(x(point2) - x(point1));
set a = sin(`ÎÏ` / 2) * sin(`ÎÏ` / 2) + cos(`Ï1`) * cos(`Ï2`) * sin(`Îλ` / 2) * sin(`Îλ` / 2);
set c = 2 * atan2(sqrt(a), sqrt(1-a));
return R * c;
END;
';
DB::unprepared($sql);
}
它有点难看,但看起来很管用…
编辑
实际上,它只会执行第一次。这个函数似乎可以将mysql函数保存到mysql数据库中。下一次“MySQL函数已经存在”时,您将有一个错误。你只需要评论上面的方块。