2017-04-20 80 views
0

假設我們有一個表:SQL,只有相同的選擇

Music track Name 
==    ==== 
1    A 
2    A 
3    B 
3    C 
3    D 
4    C 
5    E 
5    F 
6    E 
6    F 
..    .. 

我希望選擇聽誰的相同,只有相同的音樂曲目名稱。結果應該是:

select tracks, group_concat(name) as names 
from (select name, group_concat(track order by track) as tracks 
     from t 
     group by name 
    ) n 
group by tracks; 

此列出誰是正是相關的數據跟蹤和用戶的每個組合:

Name 
==== 
B (listens track 3 and only track 3) 
D (listens track 3 and only track 3) 
E (listens tracks 5, 6 and only tracks 5,6) 
F (listens tracks 5,6 and only tracks 5,6) 
.. 

回答

1

在MySQL中,我將與兩個層次聚集做到這一點名單。

+0

我想,也許是因爲你給了他們想要的信息,但不是在格式,他們想進去。 –

+0

雖然描述和問題並沒有任何匹配設置採樣結果:) –

0
select tracks, group_concat(name) as group_concated_name 
from 
(
    select name, group_concat(track order by track) as tracks 
    from t 
    group by name 
) n1 
group by tracks;