MySQL 加入 + “WHERE something=MAX(something)”
Posted
技术标签:
【中文标题】MySQL 加入 + “WHERE something=MAX(something)”【英文标题】:MySQL Join + "WHERE something=MAX(something)" 【发布时间】:2012-06-23 03:38:20 【问题描述】:我有几个需要连接的表。这些表格是:
玩家
名字 姓氏 性别 location_id (额外的不相关列)和
score_entries
得分 日期时间 player_id (额外的不相关列)我需要根据player_id
加入他们,并且只想选择players.location_id
最大的记录。所以是这样的:
SELECT
players.first_name,
players.last_name,
players.gender,
score_entries.score,
score_entries.datetime as jump_date
players.location_id,
FROM score_entries
LEFT JOIN players
ON score_entries.player_id = players.id
WHERE gender="m"
AND location_id="***(ONLY THE HIGHEST LOCATION_ID VALUE IN THE TABLE)***"
【问题讨论】:
【参考方案1】:请尝试以下查询
SELECT
players.first_name,
players.last_name,
players.gender,
score_entries.score,
score_entries.datetime as jump_date,
players.location_id
FROM score_entries
LEFT JOIN players
ON score_entries.player_id = players.id
WHERE gender="m"
AND players.location_id = (SELECT MAX(location_id) from players)
【讨论】:
以上是关于MySQL 加入 + “WHERE something=MAX(something)”的主要内容,如果未能解决你的问题,请参考以下文章