我有这个sql:
SELECT
artists.id,
actors.id,
actors.count
FROM
artists
INNER JOIN actors ON artists.title = actors.title;
Actors表有重复和一些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
答案 1 :(得分:1)
试试这个 -
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;
答案 2 :(得分: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
答案 3 :(得分: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
;
答案 4 :(得分:-1)
SELECT
artists.id,
actors.id,
MAX(actors.count)
FROM
artists
INNER JOIN actors ON artists.title = actors.title;
GROUP BY
actors.id