在MYSQL PHP中分组城市,州和国家


Grouping cities, states and countries in MYSQL PHP

我是PHP新手,为此访问了几个站点,但似乎不能完全理解。

我有这样的查询:

$result = mysql_query
  ("SELECT Country, State, City
       FROM Address_Base
       ORDER BY Country, State, City")
  or die(mysql_error());

给出如下数据示例:

加拿大-州1 -城市苹果

加拿大-第2州-城市葡萄

加拿大-州1 -城市苹果

加拿大-州2 -城市咖啡

美国-州1 -城市斑马

USA - State 2 - City Cat

USA - State 2 - City Lion

USA - State 3 - City Bird

美国-州1 -城市斑马

我的目标是将城市分组在各自的州和计数城市下,将各州分组在各自的国家下& &;把相似的国家分组,生产出这样的东西

Canada -
{
  State 1 - City Apple (2)
  State 2 - City Coffee (1), City Grapes (1)
}
USA -
{
    State 1 - City Zebra (2)
    State 2 - City Cat (1), City Lion (1)
    State 3 - City Bird (1)
}

从其他网站,我得到了这个:

$combinedResults = array();
while ($rec=mysql_fetch_assoc($result))
{
    $combinedResults[$rec['Country']][] = array
            (               
                'S' => $rec['State'], 
                'C' => $rec['City']
            );                           
}
foreach(array_keys($combinedResults) as $Country)
{ 
    echo '<div class="country">'.$Country.'</div>';
    foreach($combinedResults[$Country] as $Set)
    { 
        echo $Set['S'].'<br/>'.$Set['C'].'<br/><br/>'; 
    } 
} 

这只对相似的国家进行分组,而不包括州和城市。我猜上面的代码试图在某种多维数组中预处理数据,并有一个for循环显示结果。我不能很清楚地理解它。

如果有人能给我解释一下,我将非常感激,以及我如何进一步将上述州和城市分别分组?

SQL查询将为您提供一组具有固定列数的结果。您的示例具有可变数量的列(多个城市计数),因此这将不起作用。

您可以在SQL中使用COUNT和GROUP BY来获得类似

的内容
Country   State    Citiescount
Canada    state1      4
Canada    state2      3
USA       state2      2

等,但在左侧列中会有重复项。

  SELECT country, state, COUNT(cities)
    FROM address_base
GROUP BY country, state

要摆脱重复项,循环它以创建一个多维数组,执行类似

的操作
$countries = array();
foreach ($results as $row) {
    if (!isset($countries[$row->country])) {
        $countries[$row->country] = array();
    }
    $countries[$row->country][$row->state] = $row->citiescount;
}

或者,您可以直接回显这些内容。你可以像这样一次得到所有的结果,然后循环整个集合,或者你可以把它分解成更小的查询,每个国家或每个州一个。但是,需要注意的是,将SQL放入循环中会破坏小猫(和DB性能):)。

$result = mysql_query('SELECT Country, State, City From Address_Base ORDER BY Country, State, City') or die(mysql_error());
$countries = array();
// fetch results and build multidimensional array of city counts
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
    if(isset($countries[$row['Country']][$row['State']][$row['City']])){
        $countries[$row['Country']][$row['State']][$row['City']] += 1;
    } else{
        $countries[$row['Country']][$row['State']][$row['City']] = 1;
    }
}
// loop over all countries
foreach($countries as $country => $states){
    echo $country, PHP_EOL, '{'; // output country start
    // loop over all states
    foreach($states as $state => $cities){
        echo $state, ' - '; // output state start
        $cityCounts = array();
        foreach($cities as $city => $count){
            $cityCounts[] = $city.' ('.$count.')'; // add all city counts to array
        }
        // implode all city counts and output it
        echo implode(', ', $cityCounts);
        // output new line
        echo PHP_EOL;
    }
    // output country end
    echo PHP_EOL, '}';
}