我的 MySQL 查询出了什么问题? (使用BETWEEN)

发布于 2024-11-18 18:08:31 字数 502 浏览 1 评论 0原文

我正在尝试从数据库中选择位于用户 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

阳光下的泡沫是彩色的 2024-11-25 18:08:31

查询应该是:

  SELECT name FROM businesses 
  WHERE THE_ACTUAL_NAME_OF_THE_COLUMN BETWEEN $lat_min AND $lat_max 
  AND THE_NAME_OF_THE_NEXT_COLUMN BETWEEN $lng_min AND $lng_max 
  ORDER BY Rand() LIMIT 6

不知何故 $lat 看起来不像表中的列名。另外你还漏掉了一个AND。

The query should be:

  SELECT name FROM businesses 
  WHERE THE_ACTUAL_NAME_OF_THE_COLUMN BETWEEN $lat_min AND $lat_max 
  AND THE_NAME_OF_THE_NEXT_COLUMN BETWEEN $lng_min AND $lng_max 
  ORDER BY Rand() LIMIT 6

Somehow $lat does not look like a column name in the table. Also you were missing an AND.

A君 2024-11-25 18:08:31

看起来你缺少了 AND 例如

SELECT 
   name
FROM
   businesses
WHERE
  $lat BETWEEN $lat_min AND $lat_max
  AND $lng BETWEEN $lng_min AND $lng_max
ORDER BY Rand() LIMIT 6

Looks like you have you're missing and AND e.g.

SELECT 
   name
FROM
   businesses
WHERE
  $lat BETWEEN $lat_min AND $lat_max
  AND $lng BETWEEN $lng_min AND $lng_max
ORDER BY Rand() LIMIT 6
左岸枫 2024-11-25 18:08:31

就像其他答案一样,并且可以肯定的是,放置()。

$query = 'SELECT name 
FROM businesses 
WHERE 
('.$lat.' BETWEEN "'.$lat_min.'" AND "'.$lat_max.'")
AND ('.$lng.' BETWEEN "'.$lng_min.'" AND "'.$lng_max.'")
ORDER BY Rand() LIMIT 6";

Like, the other answers, and to be sure, place ( ).

$query = 'SELECT name 
FROM businesses 
WHERE 
('.$lat.' BETWEEN "'.$lat_min.'" AND "'.$lat_max.'")
AND ('.$lng.' BETWEEN "'.$lng_min.'" AND "'.$lng_max.'")
ORDER BY Rand() LIMIT 6";
年少掌心 2024-11-25 18:08:31
$query = "SELECT name FROM businesses WHERE $lat BETWEEN $lat_min AND $lat_max AND $lng BETWEEN $lng_min AND $lng_max ORDER BY Rand() LIMIT 6";
$query = "SELECT name FROM businesses WHERE $lat BETWEEN $lat_min AND $lat_max AND $lng BETWEEN $lng_min AND $lng_max ORDER BY Rand() LIMIT 6";
深居我梦 2024-11-25 18:08:31

假设 $lat 和 $lng 解析为实际的列名称,我认为您只是缺少一个“AND”。有时,只需格式化 SQL 语句就可以揭示问题。

SELECT name 
  FROM businesses 
  WHERE $lat BETWEEN $lat_min AND $lat_max 
    **AND** $lng BETWEEN $lng_min AND $lng_max 
ORDER BY Rand() LIMIT 6;

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.

SELECT name 
  FROM businesses 
  WHERE $lat BETWEEN $lat_min AND $lat_max 
    **AND** $lng BETWEEN $lng_min AND $lng_max 
ORDER BY Rand() LIMIT 6;
筱武穆 2024-11-25 18:08:31

我认为你需要在 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文