我目前正在从一组位置MySQL表中获取数据,这是我目前拥有的:
$query = "SELECT Name, CountryCode, Population FROM City WHERE (Population > '6000000') ORDER BY Population DESC";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td>$row[2]</td>";
echo "</tr>\n";
}
mysql_close($link);
我尝试解决的问题是如何将CountryCode转换为实际国家名称。以下是调用国家表以获取名称的示例代码。
SELECT Name FROM Country WHERE Code = 'CountryCode';
我该如何将这两个语句合并成一个?
编辑:以下是我成功实现的代码。这是否被视为一种JOIN操作?
$query = "SELECT a.Name, a.CountryCode, a.Population, b.Code, b.Name FROM City a, Country b WHERE (a.CountryCode = b.Code) AND (a.Population > '6000000') ORDER BY Population DESC";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>$row[0]</td>";
echo "<td>$row[2]</td>";
echo "<td>$row[4]</td>";
echo "</tr>\n";
}
mysql_close($link);