在比赛中返回最佳结果

时间:2013-12-22 16:20:02

标签: mysql sql

我有这样的查询

select R.EID, name, record, date, TYPE_NAME, METERS
from PLAYER P, REGISTER R, COMPETITION C, EVENT E, TYPE T, DISTANCE D
where P.PID=R.PID 
  and R.CID=C.CID 
  and R.EID=E.EID 
  and T.TYID=E.TYID 
  and D.DID=E.DID 
order by R.EID, record

它会像这样返回

EID  Name    Record       Date       Type_Name  Meters

1   HUONG   00:07:45    2011-03-22  Freestyle   100
1   NHUNG   00:16:25    2011-03-22  Freestyle   100
1   NULL    00:23:23    2011-03-22  Freestyle   100
1    HUY    00:23:43    2011-03-22  Freestyle   100
1   THAO    00:26:34    2011-03-22  Freestyle   100
1   TRANG   00:34:23    2011-03-22  Freestyle   100
1   HUNG    00:34:23    2011-03-22  Freestyle   100
1   CUONG   00:34:54    2011-03-22  Freestyle   100
1    BON    00:45:21    2011-03-22  Freestyle   100
2   HUNG    00:02:34    2011-03-22  Freestyle   800
2   NHUNG   00:15:25    2011-03-22  Freestyle   800
2    HUY    00:22:33    2011-03-22  Freestyle   800
2   TRANG   00:23:25    2011-03-22  Freestyle   800

对于比赛中的每个赛事,它会为参加该赛事的每位游泳选手多次列出EID。

我想要的是每个比赛(CID)中的每个赛事(EID),根据他们的记录得到第一,第二,第三的竞争者的水平列表。比赛有多个比赛,每场比赛都可以在多场比赛中进行,所以我通过EID和比赛日期来定义每个比赛。

我试过这个

select x.EID, name, date, TYPE_NAME, METERS from 
(select R.EID, name, date, TYPE_NAME, METERS
from PLAYER P, REGISTER R, EVENT E, TYPE T, DISTANCE D
where P.PID=R.PID and R.CID=C.CID 
  and R.EID=E.EID 
  and T.TYID=E.TYID 
  and D.DID=E.DID 
order by R.EID, record) x
group by x.EID

然后为第1,第2,第3创建3个视图并使用左连接。

我有一个想法是使用where record not in (1st record)对记录进行排序,并继续为第2和第3次做这个,但我认为如果我有相同的记录,它将消除一些结果。

我希望结果像这样

 EID    Date         First   Second  Third

    1   2013-03-22  HUONG   NHUNG   NULL
    2   2013-03-22  HUNG    NHUNG   HUY
    3   2013-11-13  TRANG   DOAN    LINH

1 个答案:

答案 0 :(得分:0)

您可以使用子查询和LIMIT关键字来执行此操作。使用LIMIT如果记录时间相同,您将不会错过任何条目。

SQL

SELECT   r.CID, r.EID, c.date,
         (SELECT   p.name 
          FROM     REGISTER rr 
          JOIN     PLAYER p on rr.PID = p.PID 
          WHERE    rr.CID = r.CID and rr.EID = r.EID
          ORDER BY rr.record LIMIT 0,1) as 'First',
         (SELECT   p.name 
          FROM     REGISTER rr 
          JOIN     PLAYER p on rr.PID = p.PID 
          WHERE    rr.CID = r.CID and rr.EID = r.EID
          ORDER BY rr.record LIMIT 1,1) as 'Second',
         (SELECT   p.name 
          FROM     REGISTER rr 
          JOIN     PLAYER p on rr.PID = p.PID 
          WHERE    rr.CID = r.CID and rr.EID = r.EID
          ORDER BY rr.record LIMIT 2,1) as 'Third'
FROM     REGISTER r
JOIN     COMPETITION c on r.CID = c.CID
GROUP BY r.CID, r.EID, c.date

架构:

CREATE TABLE COMPETITION
(
    CID INT AUTO_INCREMENT PRIMARY KEY,
    date DATETIME
);

CREATE TABLE TYPE
(
    TYID INT AUTO_INCREMENT PRIMARY KEY,
    TYPE_NAME VARCHAR(255)
);

CREATE TABLE DISTANCE
(
    DID INT AUTO_INCREMENT PRIMARY KEY,
    METERS INT
);

CREATE TABLE EVENT
(
    EID INT AUTO_INCREMENT PRIMARY KEY,
    TYID INT REFERENCES TYPE(TYID),
    DID INT REFERENCES DISTANCE(DID)
);

CREATE TABLE PLAYER
(
    PID INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) 
);

CREATE TABLE REGISTER
(
    PID INT REFERENCES PLAYER(PID),
    CID INT REFERENCES COMPETITION(CID),
    EID INT REFERENCES EVENT(EID),
    record INT
);

INSERT INTO COMPETITION (date) values ('2011-03-22');
INSERT INTO TYPE (TYPE_NAME) values ('Freestyle');
INSERT INTO DISTANCE (METERS) values (100), (800);
INSERT INTO EVENT (TYID, DID) values (1,1), (1,2);
INSERT INTO PLAYER (name) values ('HUONG'), ('NHUNG'), ('NULL'), ('HUY'), ('THAO'), ('TRANG'), ('HUNG'), ('CUONG'), ('BON');
INSERT INTO REGISTER (PID, CID, EID, record) values 
(1, 1, 1, 465),
(2, 1, 1, 985),
(3, 1, 1, 1403),
(4, 1, 1, 1423),
(5, 1, 1, 1594),
(6, 1, 1, 2063),
(7, 1, 1, 2063),
(8, 1, 1, 2094),
(9, 1, 1, 2721),
(7, 1, 2, 154),
(2, 1, 2, 925),
(4, 1, 2, 1353),
(6, 1, 2, 1405);