ПРИСОЕДИНЯЙТЕСЬ по ряду критериев - PullRequest
1 голос
/ 14 ноября 2011

У меня есть этот 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);

Ответы [ 5 ]

3 голосов
/ 14 ноября 2011

Как насчет

SELECT  actors.id
        , MAX(actors.count)
FROM    artists 
        INNER JOIN actors ON artists.title = actors.title
GROUP BY
        actors.id        
1 голос
/ 14 ноября 2011
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
1 голос
/ 14 ноября 2011

Попробуйте это -

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;
0 голосов
/ 14 ноября 2011

Простой способ:

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 
;
0 голосов
/ 14 ноября 2011
SELECT 
     artists.id, 
     actors.id, 
     MAX(actors.count) 
FROM 
     artists 
INNER JOIN actors ON artists.title = actors.title;
GROUP BY
    actors.id
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...