2016-11-25 40 views
0

我有兩列(film_id,actor_id),我需要找到一對演員,使電影中最多的相互出現,例如演員10和演員20一起有兩部電影(1 ,3),所以我需要回到10,20SQL發現最大的相互出現

the table looks simaller to this:

+0

你能設置的結構和一些數據http://sqlfiddle.com/? –

+0

我上傳了一張圖片,希望它能讓事情變得更容易 –

+0

它太大了無法處理,但是從這裏下載sakila db https://dev.mysql.com/doc/index-other.html –

回答

0

不知道這是什麼,你到底要或沒有,但嘗試它:

select 
    group_concat(actor_id) as actors 
from (
    select actor_id, group_concat(movie_id order by movie_id) as movies 
    from tbl1 
    group by actor_id) t1 
group by movies 
having count(actor_id) = 2 

SQLFiddle Demo Here

+0

hmm。我試着運行它,我得到零行返回,但「有」標籤是deffenlty一個很好的aproche,我會谷歌它有點:) –

+0

但我用你的sqlfiddle鏈接和它的工作,所以也許我的mySql錯誤? –

0

您可以group by可能沿着使用group_concat()

select group_concat(actor_id) as multistarter 
from movies 
group by movie_id; 
+0

其返回的錯誤答案,那裏是不是查詢中的最大功能,並且沒有計數 –

+0

發佈您的預期輸出... – Rahul

+0

我做了,我需要返回10,20 –