【问题标题】:Delete the row with the highest date difference删除日期差异最大的行
【发布时间】:2022-01-12 23:32:57
【问题描述】:

我有一张桌子

student_id INTEGER PRIMARY KEY AUTOINCREMENT,
name varchar(255)  NOT NULL,
surname varchar(255)  NOT NULL,
dateOfBirth DATE NOT NULL,
yearEnrolled YEAR NOT NULL);

有价值观

INSERT INTO student (name, surname, dateOfbirth, yearEnrolled)
VALUES
("Walter", "Ananas","1998-05-25",2020),
("Wander", "Ananas","1998-07-24",2019),
("Andrew", "Maxwell","1999-09-25",2020),
("Philip", "Plein","1997-05-15",2020),
("Wayne", "Johnson","1998-02-15",2020),
("Chalton", "Cole","1998-07-29",2018),
("Moise", "Kevin","2000-11-25",2021),
("Wiktor", "Watermelon","1998-12-15",2021),
("Jean-Pierre", "Care","1998-01-22",2020),
("Anthony", "Martial","1996-05-13",2018),
("Wilt", "Chamberlain","1997-05-25",2017),
("Donnavan", "Wiston","1995-05-15",2017),
("Lothar", "Mathews","1999-03-05",2020),
("Ant", "Fibonacci","2001-11-13",2021),
("Kelton", "Mansour","1997-12-15",2021),
("Jean-Pierre", "Cordone","1996-11-22",2020),
("Anthony", "Morinson","1997-05-18",2018),
("Balwain", "Chamberlain","1999-11-25",2018),
("Donnavan", "Konwar","1997-05-22",2017),
("Lothar", "Ans","1995-03-16",2016),
("Walter", "Doile","1994-05-17",2017);

我想删除年龄最大的学生所在的行。

我可以使用以下命令获取年龄,但不知何故我无法将其与 DELETE stament 结合使用

DELETE FROM student
WHERE SELECT dateOfBirth, DATE('now')- dateOfBirth as age
FROM student;

我正在使用 SQL lite

【问题讨论】:

  • 请查看我的答案的更新。

标签: sql sqlite


【解决方案1】:

如果有一些年龄相同,这将删除MAX 结果中的第一个:

DELETE FROM student
WHERE 
student_id  =
SELECT student_id , MAX(age) FROM (
SELECT student_id, cast(strftime('%Y.%m%d', 'now') - strftime('%Y.%m%d', dateOfBirth) as int) as age FROM student
)

这将删除所有 MAX 年龄:

DELETE FROM student
WHERE 
cast(strftime('%Y.%m%d', 'now') - strftime('%Y.%m%d', dateOfBirth) as int) =
SELECT MAX(age) FROM (
SELECT cast(strftime('%Y.%m%d', 'now') - strftime('%Y.%m%d', dateOfBirth) as int) as age FROM student
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    • 2020-11-12
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多