2011-11-14 29 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 
;