2

我有以下示例数据,我需要按特定日期清理记录。对于此示例,让我们使用 2012 年 3 月之前的任何内容都需要与其他相关记录一起删除。

作者表

    Author       Book     date
-------------------------------------
    Smith        Bk1      01/01/2012
    Smith        Bk2      02/01/2012
    Smith        Bk3      02/25/2012
    Johnson      Bk1      06/01/2011
    Johnson      Bk2      09/01/2011
    Johnson      Bk3      03/01/2012
    Johnson      Bk4      01/01/2013
    Jones        BK1      01/01/2001
    Jones        BK2      01/01/2002
    Jones        BK3      01/01/2009
    Jones        BK4      08/01/2013

还有一个书桌

    Author       Book     info
-------------------------------------
    Smith        Bk1      asdfadf
    Smith        Bk2      asdfasdf
    Smith        Bk3      asdfadf
    Johnson      Bk1      asdfadf
    Johnson      Bk2      adsfasdf
    Johnson      Bk3      asdasdf
    Johnson      Bk4      asdfasdf
    Jones        BK1      asdfasdf
    Jones        BK2      adsfasdf
    Jones        BK3      adsfasdf
    Jones        BK4      adsfasdf

以上是数据集。因此,如果表中的 Author 有一本书早于 2013 年 3 月 1 日,则需要删除所有记录。如果他们至少有一本比指定日期更新的书,则保留所有记录。在这种情况下,Jones 记录是唯一保留在表数据集中的记录。一旦我们确定过滤了哪个作者,我们将从 Book 表中删除记录,然后是 Author 表。

感谢您的帮助,我很感激。

4

2 回答 2

2

这似乎可以满足您的要求(我添加了代码以使用 TempDB 提供概念验证):

USE tempdb;

/* Create the same tables, and insert test data */
CREATE TABLE Authors
(
    Author nvarchar(255)
    , Book nvarchar(255)
    , BookDate datetime
);

INSERT INTO Authors VALUES ('joe','book1',GetDate()-100); --100 days old
INSERT INTO Authors VALUES ('joe','book2',GetDate()-200); --200 days old
INSERT INTO Authors VALUES ('joe','book3',GetDate()-300); --300 days old
INSERT INTO Authors VALUES ('sam','book4',GetDate()-400); --etc
INSERT INTO Authors VALUES ('sam','book5',GetDate()-500);
INSERT INTO Authors VALUES ('sam','book6',GetDate()-600);

CREATE TABLE Books
(
    Author nvarchar(255)
    , Book nvarchar(255)
    , Info nvarchar(255)
);

INSERT INTO Books VALUES ('joe','book1','asdf');
INSERT INTO Books VALUES ('joe','book2','asdfg');
INSERT INTO Books VALUES ('joe','book3','asdfh');
INSERT INTO Books VALUES ('sam','book4','asdf');
INSERT INTO Books VALUES ('sam','book5','bsdf');
INSERT INTO Books VALUES ('sam','book6','csdf');

/* This statement deletes rows from the dependent table, Books */
DELETE  
FROM Books 
FROM Books LEFT JOIN 
(       /* This selects all Authors with books in the past 300 days */
    SELECT Author 
    FROM Authors
    WHERE BookDate >= GetDate()-300 
) t2 ON Books.Author = t2.Author
WHERE t2.Author IS NULL; /* This ensures rows from Books only get deleted
                                when for Authors that DON'T have books in the
                                past 300 days */

/* This statement deletes rows from the main table, Authors */
DELETE  
FROM Authors 
FROM Authors LEFT JOIN 
(       /* This selects all Authors with books in the past 300 days */
    SELECT Author 
    FROM Authors
    WHERE BookDate >= GetDate()-300 
) t2 ON Authors.Author = t2.Author
WHERE t2.Author IS NULL; /* This ensures rows from Books only get deleted
                                when for Authors that DON'T have books in the
                                past 300 days */

SELECT * FROM Books;
SELECT * FROM Authors;

只有 Sam 的行将被删除,因为他是唯一一位没有超过 300 天新书的作者。

于 2013-09-18T22:20:19.997 回答
1

先从表1中删除:

DELETE FROM Table1
WHERE Author NOT IN (SELECT DISTINCT Author 
   FROM Table1 WHERE Date >= '2013-03-01')

然后从表2:

DELETE FROM Table2 WHERE Author NOT IN (SELECT Author FROM Table1)

不是我写过的最漂亮的 SQL,但应该足够了。如果您使用外键对表进行规范化,这样的事情就行不通,但我认为现在情​​况并非如此。

如果您坚持以相反的顺序运行它:

DELETE FROM Table2 WHERE Author NOT IN (
    SELECT DISTINCT Author 
    FROM Table1 WHERE Date >= '2013-03-01')

DELETE FROM Table1
    WHERE Author NOT IN (SELECT DISTINCT Author FROM Table2)
于 2013-09-18T22:23:06.977 回答