我有一个查询,根据一个人的id(athleteid)显示比赛结果列表。我已经查询了数据库,以便按照时间(性能)和事件(偶数)的顺序列出人员的比赛,但是我很难将其设置为子查询,以便我可以将结果分组以便让我最快每个晚上的表现。这就是我到目前为止所做的:
$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");
然而,这给了我一个错误,指出:'group statement'中的未知列't1.eventid'
非常感谢任何帮助
答案 0 :(得分: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
.......
答案 1 :(得分: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");