MySQL版本:5.7.30
简介:
今天有同学反馈获取某个用户的附近的人数据时没有数据返回,这明显不正常,接下来为排查过程。
获取附近的人的语句如下:
SELECT
FLOOR(st_distance_sphere((select location from t_user_location where user_id = 1234), location)) distance,b.*
FROM t_family b
left join `t_family_location` a
on a.family_id = b.id
where st_distance_sphere((select location from t_user_location where user_id = 1234), location) < 81843200000
AND b.public=1
ORDER BY distance asc,b.id
排查过程如下:
1. 排除了没有数据的可能性;
2. 去掉order by,有结果返回;
3. 去掉order by distance,对其它字段进行排序,也有结果返回;
问题:
为何这里对 order by distance 进行排序会导致没有结果返回呢?
查看warnings输出
> show warnings;
1210 Incorrect arguments to st_distance_sphere
查询错误代码,发现是 location字段有错误的数据。
查看官方文档:
MySQL :: MySQL 5.7 Reference Manual :: 12.17.12 Spatial Convenience Functions
The geometry arguments should consist of points that specify (longitude, latitude) coordinate values:
-
Longitude and latitude are the first and second coordinates of the point, respectively.
-
Both coordinates are in degrees.
-
Longitude values must be in the range (-180, 180]. Positive values are east of the prime meridian.
-
Latitude values must be in the range [-90, 90]. Positive values are north of the equator.
经度值必须在 (-180, 180]范围内,纬度值必须在[-90, 90]。
结果:
检查发现,有一条数据经度和纬度搞反了,纬度值超出了范围[-90, 90],修改完该条数据后,再次执行该sql,这次有数据返回了,到此,问题解决。
小结:
当发现sql执行结果没显异常,又没有报错的时候,记得执行一下show warnings,或许能发现问题所在。