PHP显示来自表的分组数据

时间:2015-12-16 02:10:13

标签: php mysql

我有这样的数据集:

Continent | Country   | City      | Population

Asia      | Indonesia | Jakarta   | 1000
Asia      | Indonesia | Denpasar  | 500
Asia      | Japan     | Tokyo     | 1500
Asia      | Japan     | Kyoto     | 1000
America   | Brazil    | Rio       | 1000
America   | Brazil    | Sao Paulo | 1500
America   | USA       | New York  | 1000
America   | USA       | Kanzas    | 500

如何在PHP中显示这样的分组数据:

Asia----------------4000
-Indonesia--------1500
--Jakarta----------1000
--Denpasar--------500
-Japan------------2500
--Tokyo-----------1500
--Kyoto-----------1000

America----------4000
-Brazil-------------2500
--Rio---------------1000
--Sao Paulo------1500
-USA--------------1500
--New York-------1000
--Kanzas-----------500

目前我正在使用多个查询:

$rs1 = mysql_query("SELECT continent, SUM(population) AS population FROM some_table GROUP BY continent");

while($c=mysql_fetch_array($rs1))
{
    //display data continent here
    $rs2 = mysql_query("SELECT country, SUM(population) AS population FROM some_table WHERE continent = $c[continent] GROUP BY country");

    while($n=mysql_fetch_array($rs2))
    {
        //display data country here
        $rs3 = mysql_query("SELECT city, population FROM some_table WHERE continent = $c[continent] AND country = $n[country]");

        while($k=mysql_fetch_array($rs3))
        {
            //display data city here
        }
    }
}

它工作正常,但我认为它对我拥有的大量数据无效。最好的方法是什么?

0 个答案:

没有答案