在 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 - 城市斑马
美国 - 州 2 - 城市猫
美国 - 州 2 - 城市之狮
美国 - 州 3 - 城市鸟
美国 - 州 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 循环显示结果。我无法清楚地理解它。
如果有人能为我解释一下,以及我如何进一步按照上述要求分别对州和城市进行分组,我将非常感激?
I am new to PHP and have visited several sites for this but can't seem to understand thoroughly.
I have this query:
$result = mysql_query
("SELECT Country, State, City
FROM Address_Base
ORDER BY Country, State, City")
or die(mysql_error());
This gives the following data sample:
Canada - State 1 - City Apple
Canada - State 2 - City Grapes
Canada - State 1 - City Apple
Canada - State 2 - City Coffee
USA - State 1 - City Zebra
USA - State 2 - City Cat
USA - State 2 - City Lion
USA - State 3 - City Bird
USA - State 1 - City Zebra
My goal is to group the cities under the respective state and count city, group the states under the respective country & group similar countries and produce something like this
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)
}
From some other site, I got this:
$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/>';
}
}
This only groups similar Countries and not states and the cities. I guess the above piece of code is trying to pre-process the data in some sort of multi-dimensional array and have a for loop display the results. I am not able to understand it clearly.
I would be very thankful if someone could explain it for me and how I can further group the states and cities respectively as sought above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SQL 查询将为您提供一组具有固定列数的结果。您的示例具有可变数量的列(多个城市计数),因此这不起作用。
您可以在 SQL 中使用 COUNT 和 GROUP BY 来获取类似的内容
,但左侧列中会有重复项。
要消除重复项,请对其进行循环以创建一个多维数组,执行类似的操作
或者,您可以只回显这些内容。您可以像这样一次性获取所有结果并循环整个集合,也可以将其分解为较小的查询并为每个国家或每个州执行一个查询。但请注意,将 SQL 放入循环会杀死小猫(和数据库性能):)。
SQL queries will give you a set of results with a fixed number of columns. Your example has a variable number of columns (multiple city counts) so this won't work.
You can use COUNT and GROUP BY in the SQL to get something like
etc, but you will have duplicates in the left hand column(s).
To get rid of the duplicates, loop over it to make an multidimensional array doing something like
Alternatively, you can just echo the stuff instead. You can either get all the results in one go like this and loop over the whole set, or you can break it into smaller queries and do one per country or one per state. Be warned, however, that putting SQL in loops kills kittens (and DB performance) :).