0

我有一个查询,它根据一个人的 id (athleteid) 显示比赛结果列表。我已经查询了数据库,以便它按时间(性能)和事件(eventid)的顺序列出该人的比赛,但我正在努力将其设置为子查询,以便之后我可以对结果进行分组,让我以最快的速度每个 eventid 的性能。这是我到目前为止所拥有的:

$id = $_GET['id'];
$query = sprintf("SELECT events.eventcode, meetings.meetingid, meetings.meetingname, meetings.location, meetings.meetingdate, DATE_FORMAT(meetings.meetingdate,'%%d %%b %%Y') AS date, results.performance, results.unit, results.wind, agegroups.agegroup
FROM results 
JOIN athletes ON athletes.athleteid = results.athleteid
JOIN meetings ON meetings.meetingid = results.meetingid
JOIN events ON events.eventid = results.eventid
JOIN agegroups ON agegroups.agegroupid = results.agegroupid
WHERE results.athleteid='$id' AND events.eventtype = 'track' ORDER BY results.eventid asc, results.performance asc, meetings.meetingdate desc");

这工作正常,并以正确的顺序显示所有相关条目。然后我尝试将其转换为子查询,以便我可以使用 GROUP BY,但没有成功。这是我尝试使用它的地方:

$id = $_GET['id'];
$query = sprintf("SELECT * FROM (SELECT events.eventcode, meetings.meetingid, meetings.meetingname, meetings.location, meetings.meetingdate, DATE_FORMAT(meetings.meetingdate,'%%d %%b %%Y') AS date, results.performance, results.unit, results.wind, agegroups.agegroup
FROM results 
JOIN athletes ON athletes.athleteid = results.athleteid
JOIN meetings ON meetings.meetingid = results.meetingid
JOIN events ON events.eventid = results.eventid
JOIN agegroups ON agegroups.agegroupid = results.agegroupid
WHERE results.athleteid='$id' AND events.eventtype = 'track' ORDER BY results.eventid asc, results.performance asc, meetings.meetingdate desc) AS t1
GROUP BY t1.eventid");

但是,这给了我一个错误,指出:“组语句”中的未知列“t1.eventid”

任何帮助是极大的赞赏

4

2 回答 2

0

您尚未eventid在子查询中选择 。如果它不存在,你怎么能分组呢?

SELECT events.eventid,events.eventcode, 
meetings.meetingid, meetings.meetingname, 
meetings.location, meetings.meetingdate, 
DATE_FORMAT(meetings.meetingdate,'%%d %%b %%Y') AS date, 
results.performance, results.unit, results.wind, 
agegroups.agegroup
.......
于 2012-05-11T11:32:44.083 回答
0

那是因为您在子查询中没有该列,请尝试在子选择中添加该列:

$id = $_GET['id'];
$query = sprintf("SELECT * FROM (SELECT events.eventid, events.eventcode, meetings.meetingid, meetings.meetingname, meetings.location, meetings.meetingdate, DATE_FORMAT(meetings.meetingdate,'%%d %%b %%Y') AS date, results.performance, results.unit, results.wind, agegroups.agegroup
FROM results 
JOIN athletes ON athletes.athleteid = results.athleteid
JOIN meetings ON meetings.meetingid = results.meetingid
JOIN events ON events.eventid = results.eventid
JOIN agegroups ON agegroups.agegroupid = results.agegroupid
WHERE results.athleteid='$id' AND events.eventtype = 'track' ORDER BY results.eventid asc, results.performance asc, meetings.meetingdate desc) AS t1
GROUP BY t1.eventid");
于 2012-05-11T11:32:59.883 回答