2011-10-22 60 views
0

我一直試圖讓這個工作使用ORDER BY和LIMIT,但它會輸出放入的所有東西。ORDER BY和LIMIT似乎不工作:PHP/Mysql - ORDER BY使用臨時表時

$lat1 = 37.349418; 
$lon1 = -121.896286; 
$distance = 25; 


$q = "SELECT * FROM cityname WHERE feature_class = 'A' OR feature_class = 'P'"; 
$r = mysql_query($q) or die(mysql_error()); 
while ($row = mysql_fetch_array($r)) { 
$lat = trim($row["latitude"]); 
$lon = trim($row["longitude"]); 
$name = $row["name"]; 
$pop = $row["population"]; 
$miles = distance($lat, $lon, $lat1, $lon1, "m"); 
$milesrounded = round($miles, 2); 
if ($miles < $distance) { 
    if ($pop > 0) { 
     $q2 = "CREATE TEMPORARY TABLE IF NOT EXISTS `templocation4` (
     `cityname` varchar(75) NOT NULL, 
     `distance` double NOT NULL 
     ) ENGINE=MyISAM DEFAULT CHARSET=latin1"; 
     $r2 = mysql_query($q2) or die(mysql_error()); 
     $q1 = "INSERT INTO templocation4 (cityname, distance) VALUES ('$name', '$milesrounded')"; 
     $r1 = mysql_query($q1) or die(mysql_error()); 
     $q3 = "SELECT MIN(distance) FROM templocation4 GROUP BY distance DESC LIMIT 10"; 
     $r3 = mysql_query($q3) or die(mysql_error()); 
     while ($row1 = mysql_fetch_array($r3)) { 
      echo $row1["cityname"]; 
      echo " "; 
      echo $row1["distance"]; 
      echo "<br>"; 
     } 
     $q5 = "DROP TABLE templocation4"; 
     $r5 = mysql_query($q5) or die(mysql_error()); 
      } 
     } 
} 

表城市名具有> 250K的條目,我試圖將其分類到基於緯度/經度用戶具有輸入最近的城市的名稱。

謝謝。

回答

0

這裏有一個錯誤:

$q3 = "SELECT MIN(distance) FROM templocation4 GROUP BY distance DESC LIMIT 10"; 
$r3 = mysql_query($q3) or die(mysql_error()); 

while ($row1 = mysql_fetch_array($r3)) { 
    echo $row1["cityname"]; // Where is cityname defined??? 
    echo " "; 
    echo $row1["distance"]; // Where is distance defined??? 
    echo "<br>"; 
} 

$ R3只有一個結果列(你有沒有一個名字)。你確定你正在使用正確的變量,因爲你的代碼沒有辦法像你發佈的那樣工作。

另外你的變量命名非常糟糕。使用有意義的名稱而不是$q1$q2等...

+0

我猜他們是有意義的測試代碼時,意思是 - $ Q3 = QUERY3等代碼的工作,它只是輸出都插入到臨時表中,而不是排序和限制結果。 – Go3Team

+1

@ Go3Team:「我猜測它們在測試代碼時意味着什麼 - $ q3 = query3等等」然後,爲什麼第一個查詢叫做'q2'而第二個查詢叫做'q1'?這毫無意義。 –

+0

q2查詢在q1之後寫入。臨時表查詢被添加來加快速度。 – Go3Team

0

請再次用邏輯檢查。 「GROUP BY距離」將從關注記錄列表中返回單個記錄。對於例如如果在10英里距離處有100個用戶,則您的查詢將獲取10英里找到的第一條記錄。這將不會返回全部100條記錄。

$q1 = "INSERT INTO templocation4 (cityname, distance) VALUES ('$name', '$milesrounded')"; 
    $r1 = mysql_query($q1) or die(mysql_error()); 
    $q3 = "SELECT MIN(distance) FROM templocation4 GROUP BY distance DESC LIMIT 10"; 
    $r3 = mysql_query($q3) or die(mysql_error()); 

需要重做你的需要和邏輯。

0
//determine distance function 
function distance($lat, $lon, $lat1, $lon1, $unit) { 

    $theta = $lon - $lon1; 
    $dist = sin(deg2rad($lat)) * sin(deg2rad($lat1)) + cos(deg2rad($lat)) * cos(deg2rad($lat1)) * cos(deg2rad($theta)); 
    $dist = acos($dist); 
    $dist = rad2deg($dist); 
    $miles = $dist * 60 * 1.1515; 
    $unit = strtoupper($unit); 

    if ($unit == "K") { 
     return ($miles * 1.609344); 
    } else if ($unit == "N") { 
     return ($miles * 0.8684); 
    } else { 
     return $miles; 
    } 
} 

//sample latitude for testing purposes 
$lat1 = 37.349418; 
//sample longitude for testing purposes 
$lon1 = -121.896286; 
//sample distance for testing purposes 
$distance = 25; 

//query to select only a or p feature class 
$query = "SELECT * FROM cityname WHERE feature_class = 'A' OR feature_class = 'P'"; 
$result = mysql_query($query) or die(mysql_error()); 
//create the temporary table - if it does not exist 
$createtemporarytablequery = "CREATE TEMPORARY TABLE IF NOT EXISTS `templocation4` (
`cityname` varchar(75) NOT NULL, 
`distance` double NOT NULL 
) ENGINE=MyISAM DEFAULT CHARSET=latin1"; 
$resultofcreatetemporarytablequery = mysql_query($createtemporarytablequery) or die(mysql_error()); 
while ($row = mysql_fetch_array($result)) { 
    //gets latitude of city in database 
    $lat = trim($row["latitude"]); 
    //gets longitude of city in database 
    $lon = trim($row["longitude"]); 
    //gets cityname 
    $name = $row["name"]; 
    //gets population of aforementioned cityname 
    $pop = $row["population"]; 
    //determines distance from sample latitude and longitude from the latitude and longitude of cities in the cityname database 
    $miles = distance($lat, $lon, $lat1, $lon1, "m"); 
    //round the miles to the 2nd decimal place 
    $milesrounded = round($miles, 2); 
    //determine if the city meets the request 
    if ($miles < $distance) { 
     //make sure its a populated city 
      if ($pop > 0) { 
       //insert stuff into temporary table 
       $insertstuffintotemporarytable = "INSERT INTO templocation4 (cityname, distance) VALUES ('$name', '$milesrounded')"; 
       $resultofinsertstuffintotemporarytable = mysql_query($insertstuffintotemporarytable) or die(mysql_error()); 
      } 
    } 
} 
//retrieve the closest 10 cities from the temporary table 
$retrieve10closestcities = "SELECT * FROM templocation4 GROUP BY distance ASC LIMIT 10"; 
$resultofretrieving10closestcities = mysql_query($retrieve10closestcities) or die(mysql_error()); 
//determine how many results there are 
$numrows = mysql_num_rows($resultofretrieving10closestcities); 
//are there more than 0 results? 
if ($numrows > 0) { 
    //loops through array 
    while ($row1 = mysql_fetch_array($resultofretrieving10closestcities)) { 
     echo $row1["cityname"]; 
     echo " "; 
     echo $row1["distance"]; 
     echo "<br>"; 
    } 
} else { 
    //echos no results found 
    echo "No results found"; 
} 
//drop temporary table 
$droptable = "DROP TABLE templocation4"; 
$resultofdroptable = mysql_query($droptable) or die(mysql_error()); 

答案是:

San Jose 0.7 
Buena Vista 2.24 
Burbank 2.65 
Santa Clara 3.25 
Fruitdale 3.33 
Alum Rock 3.97 
East Foothills 4.85 
Campbell 5.21 
Seven Trees 5.41 
Milpitas 5.48