简体   繁体   English

删除所有未更改的条目(SQL)

[英]delete all entries which didnt change (sql)

i got the following table player_teams which contains data like this 我得到了下面的表player_teams,其中包含这样的数据

| id | p_id | year | t_id |
---------------------------
| 1  | 1    | 2001 | 1    |
| 2  | 1    | 2002 | 1    |
| 3  | 1    | 2003 | 1    |
| 4  | 1    | 2004 | 3    |
| 5  | 2    | 2001 | 1    |
| 6  | 2    | 2003 | 1    |
| 7  | 2    | 2004 | 2    |
| 8  | 2    | 2006 | 3    |
| 9  | 2    | 2007 | 1    |

fiddle now 现在摆弄
fiddle result 小提琴结果

now i want to delete every entry where the t_id didnt change to the next smaller entry in the year column. 现在,我想删除t_id未更改为year列中的下一个较小条目的每个条目。 p_id has to be the same of course. p_id当然必须相同。

So in this Case id 2,3 and 6 should be deleted. 因此,在这种情况下,应删除ID 2,3和6。

1: First entry of player (dont delete) 1:玩家的第一次进入(不要删除)
2: Nothing changed to the year 2001 (delete) 2:与2001年(删除)保持不变
3: Nothing changed to the year 2002 (delete) 3:与2002年(删除)保持不变
4: t_id changed to the year 2003 (dont delete) 4:t_id更改为2003年(不要删除)
5: First entry of player (dont delete) 5:玩家的第一次进入(不要删除)
6: Nothing changed to the year 2001 (2002 is missing) (delete) 6:2001年的内容保持不变(2002年缺失)(已删除)
7: t_id changed to the year 2003 (dont delete) 7:t_id更改为2003年(不要删除)
8: t_id changed to the year 2004 (2005 is missing) (dont delete) 8:t_id更改为2004年(缺少2005年)(请勿删除)
9: t_id changed to the year 2006 (dont delete) 9:t_id更改为2006年(不要删除)

Note that all of the columns may miss specific values and are not sorted. 请注意,所有列都可能缺少特定值,因此不会进行排序。

Goal is to only have the changes of the teams a player was in, not every year. 目标是只改变球员所在的球队,而不是每年。

I dont know if its possible with sql or if i have to write a program which does it. 我不知道它是否可以使用sql或是否必须编写执行此操作的程序。

Thx 谢谢

DELETE player_teams.*
FROM
  player_teams LEFT JOIN (SELECT p_id, MIN(year) As min_year
                          FROM player_teams
                          GROUP BY p_id, t_id) p
  ON player_teams.p_id = p.p_id AND player_teams.year=p.min_year
WHERE
  p.p_id IS NULL

Please see fiddle here . 请看这里的小提琴。 Or you can use this: 或者您可以使用以下命令:

DELETE p1.*
FROM
  player_teams p1 INNER JOIN player_teams p2
  ON p1.p_id=p2.p_id
     AND p1.t_id = p2.t_id
     AND p1.year>p2.year

Fiddle is here . 小提琴在这里

Edit 编辑

If you need to keep row 9 because t_id changed to 1,2,3 and then changed back to 1, you need to use more complicated query like this: 如果由于t_id更改为1,2,3,然后又更改回1,而需要保留第9行,则需要使用更复杂的查询,如下所示:

DELETE
  player_teams.*
FROM
  player_teams INNER JOIN (
    SELECT p1.p_id, p1.year, MAX(p2.year) prev_year
    FROM
      player_teams p1 LEFT JOIN player_teams p2
      ON p1.p_id = p2.p_id AND p1.year>p2.year
    GROUP BY
      p1.p_id, p1.year) ny
  ON player_teams.p_id = ny.p_id AND
     player_teams.year = ny.year
  INNER JOIN player_teams pny
  ON ny.p_id = pny.p_id
     AND ny.prev_year=pny.year
     AND player_teams.t_id = pny.t_id

Fiddle is here . 小提琴在这里

Try this and see if it works. 试试这个,看看是否可行。

DELETE FROM Table
WHERE id IN (SELECT id FROM Table t1 
              WHERE t1.id > id AND t1.p_id = p_id AND t_id = t1.t_id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM