Mysql 通过一些标准加入
我有一个sql:Mysql 通过一些标准加入,mysql,join,Mysql,Join,我有一个sql: SELECT artists.id, actors.id, actors.count FROM artists INNER JOIN actors ON artists.title = actors.title; Actors表有重复项和一些计数器字段。所以可能会有 Tom Waits | 10 Tom Waits | 30 如何更改我的第一个sql,使其只返回那些获得了MAX(count) 差不多 SELECT
SELECT
artists.id,
actors.id,
actors.count
FROM
artists
INNER JOIN actors ON artists.title = actors.title;
Actors表有重复项和一些计数器
字段。所以可能会有
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);
怎么样
SELECT actors.id
, MAX(actors.count)
FROM artists
INNER JOIN actors ON artists.title = actors.title
GROUP BY
actors.id
怎么样
SELECT actors.id
, MAX(actors.count)
FROM artists
INNER JOIN actors ON artists.title = actors.title
GROUP BY
actors.id
试试这个-
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;
试试这个-
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;
简单的方法:
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
;
简单的方法:
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
;
此处缺少某些按条件分组。此处缺少某些按条件分组。