Php MySQL从单独的表中选择一个键

Php MySQL从单独的表中选择一个键,php,mysql,Php,Mysql,我目前正在从一组位置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 "&l

我目前正在从一组位置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);
我如何将这两个语句合并为一个

编辑:这是我要做的工作。这算是加入吗

$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);
$query=“选择a.姓名、a.国家代码、a.人口、b.代码、b.姓名(a.CountryCode=b.code)和(a.Population>'6000000'),按人口描述顺序排列”;
$result=mysqli\u查询($link,$query);
while($row=mysqli\u fetch\u数组($result)){
回声“;
回显“$row[0]”;
回显“$row[2]”;
回显“$row[4]”;
回音“\n”;
}
mysql_close($link);

您需要连接具有一个主键和一个外键的城市表和国家表


了解自然连接。这很简单。

您可以使用以下查询:

$query = "SELECT city.Name,country.Name,city.CountryCode,city.Population from city INNER JOIN country where country.CountryCode=city.CountryCode and city.population>5000 ORDER BY city.Population DESC";

这很好。

您需要了解
JOIN
。我这样做的方式是最好的方式吗?您所做的是清晰明了的(在SQL中)。“最佳”更像是对可读性的判断,这也足够有效。我会建议使用比“a”和“b”更有意义的名称,但其余的都可以。欢迎使用。您可以使用
CTRL+K
格式化代码并使其更具可读性
$query = "SELECT city.Name,country.Name,city.CountryCode,city.Population from city INNER JOIN country where country.CountryCode=city.CountryCode and city.population>5000 ORDER BY city.Population DESC";