我有两个表:Races
和RacesTimes
,我只想从Races
和中提取所有表,RacesTimes
只从每个表(来自 的列)中提取最好的(按 LIMIT 1 排序的 ASC )。Finisher
Time
RacesTimes.TotalTime
RaceID
RacesTimes
所以结果是:
Races.*
, RacesTimes.Finisher
,RacesTimes.Time
这是我做的:
SELECT
Races.*,
(
SELECT
`TotalTime`
FROM
`RacesTimes`
WHERE
`RaceID` = Races.ID
ORDER BY
`TotalTime` ASC
LIMIT 1
) AS `BestTime`,
(
SELECT
`Time`
FROM
`RacesTimes`
WHERE
`RaceID` = Races.ID
ORDER BY
`TotalTime` ASC
LIMIT 1
) AS `BestTimeS`,
(
SELECT
`Finisher`
FROM
`RacesTimes`
WHERE
`RaceID` = Races.ID
ORDER BY
`TotalTime` ASC
LIMIT 1
) AS `BestFinisher`
FROM `Races`
它正在正确提取所有内容,但是查询太长了,不能简化吗?我认为简化版本使用 LEFT JOIN 或其他类似的东西,我不知道如何使用 JOIN 查询。