gpt4 book ai didi

php - 删除具有相同列值的行

转载 作者:行者123 更新时间:2023-11-29 14:51:44 29 4
gpt4 key购买 nike

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

    $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);

最佳答案

您在错误的地方使用了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";

关于php - 删除具有相同列值的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5590814/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com