簡體   English   中英

同一列中的不同值,不同列中的輸出

[英]different values in same column, output in different columns

目前,我正在從事與一級方程式有關的項目。

這就是我的結果表結構。

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

我的目標是我希望駕駛員的所有時間都可以並排顯示,然后應該由各節的參加者下達命令。

在此之后,我應該得到如下輸出:http://www.formula1.com/content/fom-website/en/championship/results/2015-race-results/2015-japan-results/qualifying.html

根據您的問題,我知道表races_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總是有結果;
  • 對於特定年份的駕駛員來說, driveridraceid是唯一的;
  • 您想按地點訂購。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM