2013-10-25 73 views
1

我正在研究类似菜单的手风琴。我正在尝试GROUP BY多栏。我已经研究过,但没有运气。我将打破我目前的结构:GROUP BY倍数和不同

在我的数据库中,我有重复country条目和city条目。该结构看起来有点像这样(小例子):

| ID | Country | City  | 
________________________________ 

| 1 | Sweden | Stockholm | 
| 2 | Sweden | Stockholm | 
| 3 | Sweden | Lund  | 
| 4 | Sweden | Lund  | 
| 5 | Germany | Berlin  | 
| 6 | Germany | Berlin  | 
| 7 | Germany | Hamburg | 
| 8 | Germany | Hamburg | 

使用GROUP BY我可以停止的相同VALUE在我的循环重复,这里是我用这个代码:

$stmt = $db->query('SELECT country FROM table GROUP BY country'); 

虽然这工作完美。现在我想抓取City,并将手风琴中的值放下,同时不要重复这些条目。这里是我当前的代码(我用引导崩溃手风琴效果):

<ul class="retailers-list"> // list 
<?php 
$stmt = $db->query('SELECT country, city FROM retailers GROUP BY country'); 
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) : 
$country = $row['country']; 
$city = $row['city']; ?> 

<li> 
<a data-pjax="content" data-toggle="collapse" data-target="#<?= $country ?>" href="retailers.php?country=<?= $country ?>"> 
<?= $country ?></a></li> //lists all countries not repeating 

<div id="<?= $country ?>" class="collapse in"> 
<a data-pjax="content" href="retailers.php?city=<?= $city ?>"> 
<?= $city ?></a><br> // listing only 1 city at a time 
</div> 
<? endwhile ?>    
</ul> 

我遇到的问题是,它只能随声附和的一个值,所以它只能随声附和斯德哥尔摩,它不会回声隆德或任何其他值。我想这是由于GROUP BY国家造成的,所以GROUP BY国家和另一个价值可能吗?

EDIT(所需的HTML输出):

Sweden 
> Stockholm 
> Lund 
Germany 
> Berlin 
> Hamburg 

使用DISTINCT输出这个样子的,这不是我所期待的:

Sweden 
> Stockholm 
Sweden 
> Lund 
Germany 
> Berlin 
Germany 
> Hamburg 
+0

我不知道你想达到什么。你能显示所需的HTML输出吗? – SirDarius

+0

在没有任何聚合函数的情况下,使用GROUP BY是不恰当的(并且很可能是低效的)。改用DISTINCT运算符。 – Strawberry

+0

@Strawberry请参阅编辑,谢谢 – SethCodes

回答

0

只需在您的组中添加增城市:

SELECT country, city FROM retailers GROUP BY country, city 
0

因此,您需要为每个国家提供不同的关联城市列表。
如果您的结果集是不是在大的城市:全国比,你可以使用:

SELECT country, GROUP_CONCAT(DISTINCT city) FROM retailers GROUP BY country 

回声结果,看看它是如何工作的。

如果结果集是大城市:国家比,你可以简单地要么是

SELECT country, city FROM retailers GROUP BY country,city 

OR

SELECT DISTINCT country, city FROM retailers ORDER BY country,city 
+0

嗨,感谢您的时间,我确实尝试过这种方法,但问题现在在我的编辑中。 – SethCodes

+0

使用组concat并执行一些PHP将其转换为您需要的内容。这是PHP的目的。 –

+0

这个问题不知道我应该如何转换它? – SethCodes

0

好,我是PDO太愚蠢,但粗程序的方法可能这个样子......

SELECT * FROM cities2; 
+---------+------------+---------+---------+---------+ 
| city_id | city_name | lat  | lon  | country | 
+---------+------------+---------+---------+---------+ 
|  1 | New York | 40.6700 | 73.9400 | US  | 
|  2 | London  | 51.5072 | 0.1275 | UK  | 
|  3 | Kodiak  | 45.0000 | 29.0000 | US  | 
|  4 | Birmingham | 34.0000 | 45.0000 | US  | 
|  5 | Birmingham | 52.2900 | 1.5400 | UK  | 
+---------+------------+---------+---------+---------+ 

<?php 
include('../testi.inc'); 
$query = " 
SELECT DISTINCT country,city_name FROM cities2 ORDER BY country,city_name; 
"; 

$result = mysqli_query($db,$query); 
$country = ''; 

while($rows = mysqli_fetch_assoc($result)){ 
if ($country == $rows['country']){ 
echo " >".$rows['city_name']."<br>\n"; 
} else { 
echo $rows['country']."<br>\n >".$rows['city_name']."<br>\n"; 
$country = $rows['country']; 
} 
} // end of while loop 
?> 

输出:

UK<br> 
    >Birmingham<br> 
    >London<br> 
US<br> 
    >Birmingham<br> 
    >Kodiak<br> 
    >New York<br> 
+0

我被困在这一个2天,我看到你在这里做了什么,但它似乎回声2结果,我不能分裂结果崩溃。任何与此有关的事情都是一个开始...... – SethCodes

+0

对不起,我没有关注。 – Strawberry