删除具有相同列值的行

发布于 2024-10-31 03:36:00 字数 650 浏览 1 评论 0原文

我正在从邮政编码表中查询一堆城市。我只想通过独特的“城市”获取结果。 (很多城市都有多个邮政编码。)这怎么办?

    $query = sprintf("SELECT `Zip`, `City`, `State`, `Lat`, `Long`, 
             ( 3959 * acos( cos( radians('%s') ) * cos( radians( `Lat` ) ) * 
             cos( radians( `Long` ) - radians('%s') ) +
             sin( radians('%s') ) * sin( radians( `Lat` ) ) ) ) AS distance 
             FROM Zips HAVING distance < '%s' ORDER BY distance LIMIT 0 , 18",
      mysql_real_escape_string($lat),
      mysql_real_escape_string($lng),
      mysql_real_escape_string($lat),
      mysql_real_escape_string($radius));
    $result = mysql_query($query, $dbConn);

I'm querying a bunch of cities from a zip-code table. I'd only like to grab results with an unique 'City'. (Lots of cities have multiple zips.) How could this be done?

    $query = sprintf("SELECT `Zip`, `City`, `State`, `Lat`, `Long`, 
             ( 3959 * acos( cos( radians('%s') ) * cos( radians( `Lat` ) ) * 
             cos( radians( `Long` ) - radians('%s') ) +
             sin( radians('%s') ) * sin( radians( `Lat` ) ) ) ) AS distance 
             FROM Zips HAVING distance < '%s' ORDER BY distance LIMIT 0 , 18",
      mysql_real_escape_string($lat),
      mysql_real_escape_string($lng),
      mysql_real_escape_string($lat),
      mysql_real_escape_string($radius));
    $result = mysql_query($query, $dbConn);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

蓝戈者 2024-11-07 03:36:01

您在错误的地方使用了HAVING,这里需要WHERE,并使用GROUP BY按城市对结果进行分组

 $query = sprintf("SELECT `Zip`, `City`, `State`, `Lat`, `Long`, ( 3959 * acos( cos(
 radians('%s') ) * cos( radians( `Lat` ) ) * cos( radians( `Long` ) - radians('%s') ) + sin(
 radians('%s') ) * sin( radians( `Lat` ) ) ) ) AS distance FROM Zips WHERE distance < '%s'  
 GROUP BY City ORDER BY distance LIMIT 0 , 18",

编辑

您也可以尝试

     $query = sprintf("SELECT `Zip`, `City`, `State`, `Lat`, `Long`, ( 3959 * acos( cos(
     radians('%s') ) * cos( radians( `Lat` ) ) * cos( radians( `Long` ) - radians('%s') ) + sin(
     radians('%s') ) * sin( radians( `Lat` ) ) ) ) AS distance FROM Zips HAVING distance < '%s'  
     GROUP BY City  ORDER BY distance LIMIT 0 , 18";

$query = sprintf("SELECT * FROM (SELECT `Zip`, `City`, `State`, `Lat`, `Long`, ( 3959 * acos( cos(
     radians('%s') ) * cos( radians( `Lat` ) ) * cos( radians( `Long` ) - radians('%s') ) + sin(
     radians('%s') ) * sin( radians( `Lat` ) ) ) ) AS distance FROM Zips HAVING distance < '%s'  
     ORDER BY distance LIMIT 0 , 18 ) as result_set GROUP BY City";

You are using HAVING at a wrong place WHERE is needed here and use GROUP BY to group result by city

 $query = sprintf("SELECT `Zip`, `City`, `State`, `Lat`, `Long`, ( 3959 * acos( cos(
 radians('%s') ) * cos( radians( `Lat` ) ) * cos( radians( `Long` ) - radians('%s') ) + sin(
 radians('%s') ) * sin( radians( `Lat` ) ) ) ) AS distance FROM Zips WHERE distance < '%s'  
 GROUP BY City ORDER BY distance LIMIT 0 , 18",

EDIT

You could also try

     $query = sprintf("SELECT `Zip`, `City`, `State`, `Lat`, `Long`, ( 3959 * acos( cos(
     radians('%s') ) * cos( radians( `Lat` ) ) * cos( radians( `Long` ) - radians('%s') ) + sin(
     radians('%s') ) * sin( radians( `Lat` ) ) ) ) AS distance FROM Zips HAVING distance < '%s'  
     GROUP BY City  ORDER BY distance LIMIT 0 , 18";

OR

$query = sprintf("SELECT * FROM (SELECT `Zip`, `City`, `State`, `Lat`, `Long`, ( 3959 * acos( cos(
     radians('%s') ) * cos( radians( `Lat` ) ) * cos( radians( `Long` ) - radians('%s') ) + sin(
     radians('%s') ) * sin( radians( `Lat` ) ) ) ) AS distance FROM Zips HAVING distance < '%s'  
     ORDER BY distance LIMIT 0 , 18 ) as result_set GROUP BY City";
笨笨の傻瓜 2024-11-07 03:36:01

尝试使用

SELECT DISTINCT(city)

OR

GROUP BY (City)

try with

SELECT DISTINCT(city)

OR

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