2013-10-25 94 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