简体   繁体   English

获取所有早于 X 天的 SQL Server 数据库记录

[英]Getting all SQL Server database records older than X days

I am working on a query to delete records from the websiteTestLocation table (where websiteSnapshotStartTime is older than X days).我正在处理从 websiteTestLocation 表中删除记录的查询(其中 websiteSnapshotStartTime 早于 X 天)。

The (truncated) table structures look like this: (截断的)表结构如下所示:

Table name表名 Column柱子
websiteSnapshot网站快照 websiteSnapshotRecordId网站快照记录 ID
websiteSnapshot网站快照 websiteSnapshotStartTime网站快照开始时间
website网站 websiteSnapshotRecordId网站快照记录 ID
website网站 websiteRecordId网站记录 ID
websiteTestLocation网站测试位置 websiteRecordId网站记录 ID

The websiteTestLocation table's "websiteRecordId" is linked to the same column in the website table and website table's "websiteSnapshotRecordId" is linked to the same column in the websiteSnapshot table. websiteTestLocation 表的“websiteRecordId”链接到网站表中的同一列,网站表的“websiteSnapshotRecordId”链接到 websiteSnapshot 表中的同一列。

I can get all of the websiteSnapshot records (older than 1 day) using:我可以使用以下方法获取所有 websiteSnapshot 记录(超过 1 天):

SELECT (websiteSnapshotId)
    FROM [dbo].websiteSnapshot
    WHERE websiteSnapshotStartTime IN (
        SELECT
            (websiteSnapshotStartTime)
        FROM
            [dbo].websiteSnapshot
        WHERE
            websiteSnapshotStartTime < DATEADD(day, -1, GETDATE())
    )

But when I include that in the rest of my query, I get the error:但是当我将其包含在我的其余查询中时,我得到了错误:

Subquery returned more than 1 value.子查询返回超过 1 个值。 This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.当子查询跟随 =、!=、<、<=、>、>= 或子查询用作表达式时,这是不允许的。

The whole query looks like this:整个查询如下所示:

--DELETE FROM [dbo].websiteTestLocation wtl
select * FROM [dbo].websiteTestLocation wtl
LEFT JOIN [dbo].website w ON w.websiteRecordId = wtl.websiteRecordId
LEFT JOIN [dbo].websiteSnapshot snap ON snap.websiteSnapshotId IN (w.websiteSnapshotId)
WHERE (SELECT (websiteSnapshotId)
    FROM [dbo].websiteSnapshot
    WHERE websiteSnapshotStartTime IN (
        SELECT 
            (websiteSnapshotStartTime)
        FROM
            [dbo].websiteSnapshot
        WHERE
            websiteSnapshotStartTime < DATEADD(day, -1, GETDATE())
    )) = snap.websiteSnapshotId
GO

I understand that, because a "foreach" loop would help, I must be doing something wrong.我明白,因为“foreach”循环会有所帮助,所以我一定做错了什么。 If I put "MAX" in front of "websiteSnapshotId" on line 5 and in front of websiteSnapshotStartTime on line 9, then I get data, but not all of the expected rows.如果我将“MAX”放在第 5 行的“websiteSnapshotId”前面和第 9 行的 websiteSnapshotStartTime 前面,那么我会得到数据,但不是所有预期的行。 I only get data from records with the "newest" websiteSnapshotStartTime that is older than 1 day.我只从“最新”网站SnapshotStartTime 早于 1 天的记录中获取数据。

Break down your query bit by bit.一点一点地分解你的查询。

On the face of it this bit looks fine从表面上看,这一点看起来不错

select * FROM [dbo].websiteTestLocation wtl
LEFT JOIN [dbo].website w ON w.websiteRecordId = wtl.websiteRecordId

But that next JOIN is all over the place.但是下一个 JOIN 无处不在。 It looks like you are trying to limit a LEFT OUTER JOIN to just the records that are in table website看起来您正试图将 LEFT OUTER JOIN 限制为仅表website中的记录

LEFT JOIN [dbo].websiteSnapshot snap ON snap.websiteSnapshotId IN (w.websiteSnapshotId)

Use an INNER JOIN instead and this ON clause改用INNER JOIN和这个ON子句

ON snap.websiteSnapshotId = w.websiteSnapshotId

Next your WHERE clause - which is where the problem you report comes from.接下来是您的WHERE子句 - 这是您报告的问题的来源。 You are trying to match a single value to a list of values, that will never work.您正在尝试将单个值与值列表匹配,这将永远无法正常工作。 All of this所有这些

WHERE (SELECT (websiteSnapshotId)
    FROM [dbo].websiteSnapshot
    WHERE websiteSnapshotStartTime IN (
        SELECT 
            (websiteSnapshotStartTime)
        FROM
            [dbo].websiteSnapshot
        WHERE
            websiteSnapshotStartTime < DATEADD(day, -1, GETDATE())
    )) = snap.websiteSnapshotId

can be replaced with可以替换为

WHERE websiteSnapshotStartTime < DATEADD(day, -1, GETDATE())

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

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