2011-11-14 27 views
1

的我有這個SQL連接:由多個指標分析

SELECT 
    artists.id, 
    actors.id, 
    actors.count 
FROM 
    artists 
INNER JOIN actors ON artists.title = actors.title; 

演員表都得到了重複和一些counter領域。所以不可能有

Tom Waits | 10 
Tom Waits | 30 

我怎樣才能改變我的第一個SQL所以它會返回只有那些演員,誰已經得到MAX(count)

喜歡的東西

SELECT 
    artists.id, 
    actors.id, 
    actors.count 
FROM 
    artists 
INNER JOIN actors ON artists.title = actors.title 
HAVING MAX(actors.count); 
+0

最多投票的湯姆等待! –

回答

3

如何

SELECT actors.id 
     , MAX(actors.count) 
FROM artists 
     INNER JOIN actors ON artists.title = actors.title 
GROUP BY 
     actors.id   
+0

就是這樣:)謝謝 – fl00r

+0

你好。 –

-1
SELECT 
    artists.id, 
    actors.id, 
    MAX(actors.count) 
FROM 
    artists 
INNER JOIN actors ON artists.title = actors.title; 
GROUP BY 
    actors.id 
+0

一些GROUP BY條件在這裏丟失。 – Benoit

1

Try t他的一個 -

SELECT artists.id, actors.id, actors.`count` FROM artists 
    INNER JOIN actors 
    ON artists.title = actors.title 
    INNER JOIN (
    SELECT title, MAX(`count`) max_count FROM actors 
     GROUP BY title 
    ) actors2 
    ON actors.title = actors2.title AND actors.`count` = actors2.max_count; 
1
SELECT artists.id, a.id, a.count 
FROM artists 
INNER JOIN (SELECT actors.id, actors.count 
      FROM actors 
      HAVING MAX(actors.count)) a ON artists.title = a.title 
+0

有趣的方法,謝謝! – fl00r

0

最簡單的辦法:

SELECT 
    artists.id, 
    (select actors.id 
    from actors 
    where artists.title = actors.title 
    order by actors.count desc Limit 1) 
    as actors_id, 
    (select actors.count 
    from actors 
    where artists.title = actors.title 
    order by actors.count desc Limit 1) 
    as actors.count 
FROM 
    artists 
;