根据组内的最小日期更新表

时间:2015-07-23 17:50:38

标签: mysql sql

我有以下表格模式,代表一些玩家及其参与的团队:

CREATE TABLE PLAYERS
(
  NAME VARCHAR(64) NOT NULL, 
  BIRTHDAY TIMESTAMP NOT NULL, 
  TEAM VARCHAR(64) NOT NULL, 
  CAPTAIN BOOLEAN
);

我的数据看起来像这样

"PLAYER1","1998-02-13 00:00:00","TEAM_A",NULL
"PLAYER2","1984-01-13 00:00:00","TEAM_A","1" 
"PLAYER3","1985-07-13 00:00:00","TEAM_A",NULL
"PLAYER4","1979-08-13 00:00:00","TEAM_B",NULL
"PLAYER5","1986-09-13 00:00:00","TEAM_B",NULL
"PLAYER6","1990-11-13 00:00:00","TEAM_B",NULL
"PLAYER7","1993-12-13 00:00:00","TEAM_C",NULL
"PLAYER8","1987-05-13 00:00:00","TEAM_C",NULL
"PLAYER9","1995-04-13 00:00:00","TEAM_C",NULL

现在我要求每个团队只需要一名队长。 TEAM_A已经有一个(PLAYER2),但TEAM_B和TEAM_C没有。所以我需要一个SQl脚本,它可以识别一个团队中最老的玩家并为他们设置队长标志。任何人都可以帮助我。

4 个答案:

答案 0 :(得分:3)

试试这个

UPDATE players p1 
SET    captain = 1 
WHERE  birthday = (SELECT Min(birthday) 
                   FROM   players p2 
                   WHERE  p1.team = p2.team) 
AND CAPTAIN <> 1

答案 1 :(得分:2)

我首先要让每个队伍中最年长的球员没有像这样的队长:

SELECT team, MIN(birthday) AS minBirthday
FROM myTable
WHERE team NOT IN (SELECT DISTINCT team FROM myTable WHERE captain = 1)
GROUP BY team;

完成后,您可以使用它来使用JOIN更新队长:

UPDATE myTable m
JOIN(
  SELECT team, MIN(birthday) AS minBirthday
  FROM myTable
  WHERE team NOT IN (SELECT DISTINCT team FROM myTable WHERE captain = 1)
  GROUP BY team) t ON t.team = m.team AND t.minBirthday = m.birthday
SET m.captain = 1;

如上所述,如果两名玩家共享相同的最小生日,则会设置两名队长。如果您有另一个决胜局,您可以调整内部查询以选择正确的播放器,并在必要时调整连接。这是一个SQL Fiddle示例。

答案 2 :(得分:0)

更新为不再是SQL Server代码,并且第一次学习了一些MySQL。

SQL Fiddle

CREATE TABLE PLAYERS
(
  NAME VARCHAR(64) NOT NULL, 
  BIRTHDAY DATETIME NOT NULL, 
  TEAM VARCHAR(64) NOT NULL, 
  CAPTAIN BIT
);

INSERT INTO PLAYERS (BIRTHDAY,CAPTAIN,TEAM,NAME) SELECT '1998-02-13 00:00:00',NULL,'TEAM_A','PLAYER1';
INSERT INTO PLAYERS (BIRTHDAY,CAPTAIN,TEAM,NAME) SELECT '1984-01-13 00:00:00',1,'TEAM_A','PLAYER2';
INSERT INTO PLAYERS (BIRTHDAY,CAPTAIN,TEAM,NAME) SELECT '1985-07-13 00:00:00',NULL,'TEAM_A','PLAYER3';
INSERT INTO PLAYERS (BIRTHDAY,CAPTAIN,TEAM,NAME) SELECT '1979-08-13 00:00:00',NULL,'TEAM_B','PLAYER4';
INSERT INTO PLAYERS (BIRTHDAY,CAPTAIN,TEAM,NAME) SELECT '1986-09-13 00:00:00',NULL,'TEAM_B','PLAYER5';
INSERT INTO PLAYERS (BIRTHDAY,CAPTAIN,TEAM,NAME) SELECT '1990-11-13 00:00:00',NULL,'TEAM_B','PLAYER6';
INSERT INTO PLAYERS (BIRTHDAY,CAPTAIN,TEAM,NAME) SELECT '1993-12-13 00:00:00',NULL,'TEAM_C','PLAYER7';
INSERT INTO PLAYERS (BIRTHDAY,CAPTAIN,TEAM,NAME) SELECT '1987-05-13 00:00:00',NULL,'TEAM_C','PLAYER8';
INSERT INTO PLAYERS (BIRTHDAY,CAPTAIN,TEAM,NAME) SELECT '1995-04-13 00:00:00',NULL,'TEAM_C','PLAYER9';

set @rownum=0;
SET @TEAM_names:='';

UPDATE Players p
JOIN(
  SELECT
     @rownum:=CASE WHEN @TEAM_names=TEAM THEN @rownum+1 ELSE 1 END as Row,
     @TEAM_names:=TEAM AS TEAM,
     name,birthday,captain
  FROM Players
  ORDER BY TEAM,BIRTHDAY
) ord on ord.NAME=p.name
    AND ord.BIRTHDAY=p.BIRTHDAY
    AND ord.TEAM=p.team
SET p.Captain= CASE WHEN Row=1 THEN true ELSE false END

SELECT *
FROM PLAYERS
ORDER BY TEAM,BIRTHDAY;

答案 3 :(得分:0)

使用此UPDATE语句查找队长:

UPDATE PLAYERS
SET
  CAPTAIN = 1
WHERE
  BIRTHDAY = (select min(BIRTHDAY) from PLAYERS where PLAYERS.TEAM = TEAM group by TEAM)