1

特定の緯度と経度から特定の距離内にある都市を見つける簡単なクエリがあります。

$query = "SELECT city,state,((ACOS(SIN($lat * PI() / 180) * SIN(lat * PI() / 180) + COS($lat * PI() / 180) * COS(lat * PI() / 180) * COS(($lon - lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS distance FROM us_cities WHERE population<>'' HAVING distance<=30 ORDER BY distance ASC limit 1, 10";

そして、結果を表示します。

$rows = mysqli_num_rows($result);           
for ($i = 0; $i < $rows; ++$i) {            
$row = mysqli_fetch_assoc($result);
echo $i+1;
echo $row['city'] . "<br />";
echo $row['state'] . "<br />";
}

私がやりたいのは、結果をエコーする代わりに、その配列を取得し、別のテーブルにそれらの都市に関する情報を含む別のクエリを実行してから、結果をエコーすることです。10個のサブクエリを実行せずにこれを実行できる方法はありますか?

Select * FROM table2 WHERE city=city AND state=state;

「table1から都市と州の配列を取得し、その配列を使用してtable2からすべてを選択するにはどうすればよいですか?

4

1 に答える 1

4

2つの間の単純なINNER JOINものが仕事をします:

SELECT 
  /* Need to add table name or alias in select list since both tables have these cols */
  us_cities.city,
  us_cities.state,
  ((ACOS(SIN($lat * PI() / 180) * SIN(lat * PI() / 180) + COS($lat * PI() / 180) * COS(lat * PI() / 180) * COS(($lon - lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS distance 
FROM
   us_cities
   /* Join on city and state */
   INNER JOIN table2 
      ON us_cities.city = table2.cities 
      AND us_cities.state = table2.state
WHERE population <>''
HAVING distance<=30
ORDER BY distance ASC 
limit 1, 10
于 2012-06-17T19:40:59.833 に答える