Mysql 同一列中的不同值,在不同列中输出

Mysql 同一列中的不同值,在不同列中输出,mysql,Mysql,目前我正在做一个与F1有关的项目 这就是我的结果表结构 CREATE TABLE IF NOT EXISTS `races_results` ( `resultid` int(11) NOT NULL, `seasonyear` int(4) NOT NULL, `trackid` tinyint(2) NOT NULL, `raceid` int(2) NOT NULL, `session` tinyint(1) NOT NULL, `q` int(11) NOT NU

目前我正在做一个与F1有关的项目

这就是我的结果表结构

CREATE TABLE IF NOT EXISTS `races_results` (
  `resultid` int(11) NOT NULL,
  `seasonyear` int(4) NOT NULL,
  `trackid` tinyint(2) NOT NULL,
  `raceid` int(2) NOT NULL,
  `session` tinyint(1) NOT NULL,
  `q` int(11) NOT NULL,
  `place` tinyint(2) NOT NULL,
  `driverid` int(2) NOT NULL,
  `teamid` int(2) NOT NULL,
  `time` int(11) NOT NULL,
  `laps` int(2) NOT NULL,
  `status` varchar(3) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
我最大的问题是我没有得到我想要的结果

SELECT place, driverid, teamid, if(q=1, time, '') as time1, if(q=2, time, '') as time2, if(q=3, time, '') as time3 
FROM `races_results` 
WHERE `seasonyear` = 2015 AND `raceid` = 3 AND `session` = 2 AND `q` IN (1,2,3) 
GROUP BY driverid 
ORDER BY CASE WHEN q = 3 THEN place >= 1 AND place <= 10 END ASC, CASE WHEN q = 2 THEN place >= 11 AND place <= 16 END ASC, CASE WHEN q = 1 THEN place >= 17 AND place <= 22 END ASC
我的目标是,我希望一个司机的所有时间将显示并排,并在此之后,应该由各部分的参与者订购


在这之后,我应该有一个这样的输出,根据你的问题,我知道表格races\u results有一行用于每个结果,因此不同资格的时间在不同的行上。要在一行上获取这些,可以对同一个表进行联接:

SELECT place, driverid, teamid, r1.time as time1, r2.time as time2, r3.time as time3
FROM races_results r1 LEFT JOIN races_results r2 on (r1.driverid=r2.driverid and r1.raceid=r2.raceid)
LEFT JOIN races_results r3 on (r1.driverid=r3.driverid and r1.raceid=r3.raceid)
WHERE r1.q=1 AND r2.q=2 AND r3.q=3 AND
`seasonyear` = 2015 AND `raceid` = 3 AND `session` = 2 
GROUP BY driverid 
ORDER BY place;
我假设:

对于q=1,总是有一个结果; driverid和raceid对于特定年份的赛车手来说是独一无二的; 你要按地点点菜。
谢谢,这让我离目标更近了。现在我只需要看看如何得到时间2和时间3的q2和q3时间。目前,我得到了时间2和时间3的练习时间。我更新了我的答案,因为我忘记添加:r1.q=1和r2.q=2和r3.q=3。我的方法是通过子查询。现在问题解决了。谢谢你的帮助。