t_dealer 包含dealer_name、dealer_state dealer_postcodet_postcodes包含 p1、p2、dist在查找距离时,我始终必须确保较低的(数字上)邮政编码为 p1,较高的邮政编码为 p2(除非它们相等)。
前问题:如果客户浏览网站并输入邮政编码(例如)3000,我如何进行连接(或我需要做的任何其他事情)来提供距离?
我知道这并不太难 - 只是做
if($postcode>$dealer_postcode){
$query="select dist from t_postcodes where p1=$dealer_postcode and p2=$postcode limit 1";
} else {
$query="select dist from t_postcodes where p2=$dealer_postcode and p1=$postcode limit 1";
}
实际问题:我需要将状态 (dealer_state) 中的所有经销商列在一个表格中(带有 jqgrid),这意味着我需要 1 个查询来提取所有经销商及其与$postcode各自的距离。
我已经尝试了各种连接和其他东西,但甚至没有接近。
谢谢你的时间。
我使用以下文章为另一个项目(请注意,它是ZipCodes)做了类似的事情。 您可以采用相同的原则并将其应用于邮政编码。
使用 MySQL 进行邮政编码半径搜索
这是答案的第一部分:
select dist,p1,p2,dealer_postcode from t_dealer, t_postcodes where (if(dealer_postcode<$user_postcode,p1,p2)=dealer_postcode) and (if(dealer_postcode<$user_postcode,p2,p1)=$user_postcode)
我主要从这个页面得到它(关于子查询)。
第二部分 - 使其运行而不超时或花费太长时间的唯一方法是限制结果。我最初有大约 2100 个经销商。我将其(通过其他标准)限制为 30,然后它运行正常