我的 MySQL 查询出了什么问题? (使用BETWEEN)
我正在尝试从数据库中选择位于用户 10 英里半径范围内的企业。测试表明问题出在我的查询上(尽管没有抛出 MySQL 错误)。变量 $lat 和 $lng 是从 Javascript 导入的,它们也很好。我怀疑这个问题与我使用 BETWEEN 有关,因为这是我第一次使用它。有什么想法吗?
$r = 20; // in miles
$lat_min = $lat - ($r / 69);
$lat_max = $lat + ($r / 69);
$lng_min = $lng - $r / abs(cos(deg2rad($lat)) * 69);
$lng_max = $lng + $r / abs(cos(deg2rad($lat)) * 69);
$query = "SELECT name FROM businesses WHERE $lat BETWEEN $lat_min AND $lat_max $lng BETWEEN $lng_min AND $lng_max ORDER BY Rand() LIMIT 6";
I'm trying to select businesses from my database who fall within a 10mi radius of the user. Testing shows that the problem lies in my query (though no MySQL error is thrown). The variables $lat and $lng are imported from Javascript and they, too, are fine. I suspect the issue is something to do with my use of BETWEEN, as this is the first time I've used it. Any ideas?
$r = 20; // in miles
$lat_min = $lat - ($r / 69);
$lat_max = $lat + ($r / 69);
$lng_min = $lng - $r / abs(cos(deg2rad($lat)) * 69);
$lng_max = $lng + $r / abs(cos(deg2rad($lat)) * 69);
$query = "SELECT name FROM businesses WHERE $lat BETWEEN $lat_min AND $lat_max $lng BETWEEN $lng_min AND $lng_max ORDER BY Rand() LIMIT 6";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
查询应该是:
不知何故 $lat 看起来不像表中的列名。另外你还漏掉了一个AND。
The query should be:
Somehow $lat does not look like a column name in the table. Also you were missing an AND.
看起来你缺少了
AND
例如Looks like you have you're missing and
AND
e.g.就像其他答案一样,并且可以肯定的是,放置()。
Like, the other answers, and to be sure, place ( ).
假设 $lat 和 $lng 解析为实际的列名称,我认为您只是缺少一个“AND”。有时,只需格式化 SQL 语句就可以揭示问题。
Assuming $lat and $lng resolve to actual column names, I think you're just missing an "AND". Sometimes, just formatting the SQL statement can reveal the issue.
我认为你需要在 WHERE 子句中使用 AND ...
WHERE ($lat BETWEEN $lat_min AND $lat_max) AND ($lng BETWEEN $lng_min AND $lng_max)
I think you need an AND in the WHERE clause...
WHERE ($lat BETWEEN $lat_min AND $lat_max) AND ($lng BETWEEN $lng_min AND $lng_max)