2

我有几个需要连接的表。这些表是:

球员

  • 性别
  • location_id
  • (其他不相关的列)

score_entry

  • 分数
  • 约会时间
  • 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)***"
4

1 回答 1

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)
于 2012-06-23T03:56:52.613 回答