获取所有 SQL 早于 X 天的服务器数据库记录
Getting all SQL Server database records older than X days
我正在查询以从 websiteTestLocation table(其中 websiteSnapshotStartTime 早于 X 天)删除记录。
(截断的)table 结构如下所示:
Table name
Column
websiteSnapshot
websiteSnapshotRecordId
websiteSnapshot
websiteSnapshotStartTime
website
websiteSnapshotRecordId
website
websiteRecordId
websiteTestLocation
websiteRecordId
网站测试位置table的“websiteRecordId”链接到网站table中的同一列,网站table的“websiteSnapshotRecordId”链接到网站table中的同一列网站快照 table.
我可以使用以下方法获取所有网站快照记录(早于 1 天):
SELECT (websiteSnapshotId)
FROM [dbo].websiteSnapshot
WHERE websiteSnapshotStartTime IN (
SELECT
(websiteSnapshotStartTime)
FROM
[dbo].websiteSnapshot
WHERE
websiteSnapshotStartTime < DATEADD(day, -1, GETDATE())
)
但是当我将其包含在其余查询中时,出现错误:
Subquery returned more than 1 value. This is not permitted when the
subquery follows =, !=, <, <= , >, >= or when the subquery is used as
an expression.
整个查询如下所示:
--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
我明白,因为“foreach”循环会有所帮助,所以我一定是做错了什么。如果我将“MAX”放在第 5 行的“websiteSnapshotId”前面和第 9 行的 websiteSnapshotStartTime 前面,那么我会得到数据,但不是所有预期的行。我只从“最新的”websiteSnapshotStartTime 早于 1 天的记录中获取数据。
一点一点地分解您的查询。
从表面上看,这一点看起来不错
select * FROM [dbo].websiteTestLocation wtl
LEFT JOIN [dbo].website w ON w.websiteRecordId = wtl.websiteRecordId
但是下一个 JOIN 到处都是。看起来您正在尝试将 LEFT OUTER JOIN 限制为仅 table website
中的记录
LEFT JOIN [dbo].websiteSnapshot snap ON snap.websiteSnapshotId IN (w.websiteSnapshotId)
改用 INNER JOIN
和这个 ON
子句
ON snap.websiteSnapshotId = w.websiteSnapshotId
接下来是您的 WHERE
子句 - 这是您报告的问题的来源。您试图将单个值与值列表匹配,这将永远行不通。所有这些
WHERE (SELECT (websiteSnapshotId)
FROM [dbo].websiteSnapshot
WHERE websiteSnapshotStartTime IN (
SELECT
(websiteSnapshotStartTime)
FROM
[dbo].websiteSnapshot
WHERE
websiteSnapshotStartTime < DATEADD(day, -1, GETDATE())
)) = snap.websiteSnapshotId
可以替换为
WHERE websiteSnapshotStartTime < DATEADD(day, -1, GETDATE())
我正在查询以从 websiteTestLocation table(其中 websiteSnapshotStartTime 早于 X 天)删除记录。
(截断的)table 结构如下所示:
Table name | Column |
---|---|
websiteSnapshot | websiteSnapshotRecordId |
websiteSnapshot | websiteSnapshotStartTime |
website | websiteSnapshotRecordId |
website | websiteRecordId |
websiteTestLocation | websiteRecordId |
网站测试位置table的“websiteRecordId”链接到网站table中的同一列,网站table的“websiteSnapshotRecordId”链接到网站table中的同一列网站快照 table.
我可以使用以下方法获取所有网站快照记录(早于 1 天):
SELECT (websiteSnapshotId)
FROM [dbo].websiteSnapshot
WHERE websiteSnapshotStartTime IN (
SELECT
(websiteSnapshotStartTime)
FROM
[dbo].websiteSnapshot
WHERE
websiteSnapshotStartTime < DATEADD(day, -1, GETDATE())
)
但是当我将其包含在其余查询中时,出现错误:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
整个查询如下所示:
--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
我明白,因为“foreach”循环会有所帮助,所以我一定是做错了什么。如果我将“MAX”放在第 5 行的“websiteSnapshotId”前面和第 9 行的 websiteSnapshotStartTime 前面,那么我会得到数据,但不是所有预期的行。我只从“最新的”websiteSnapshotStartTime 早于 1 天的记录中获取数据。
一点一点地分解您的查询。
从表面上看,这一点看起来不错
select * FROM [dbo].websiteTestLocation wtl
LEFT JOIN [dbo].website w ON w.websiteRecordId = wtl.websiteRecordId
但是下一个 JOIN 到处都是。看起来您正在尝试将 LEFT OUTER JOIN 限制为仅 table website
LEFT JOIN [dbo].websiteSnapshot snap ON snap.websiteSnapshotId IN (w.websiteSnapshotId)
改用 INNER JOIN
和这个 ON
子句
ON snap.websiteSnapshotId = w.websiteSnapshotId
接下来是您的 WHERE
子句 - 这是您报告的问题的来源。您试图将单个值与值列表匹配,这将永远行不通。所有这些
WHERE (SELECT (websiteSnapshotId)
FROM [dbo].websiteSnapshot
WHERE websiteSnapshotStartTime IN (
SELECT
(websiteSnapshotStartTime)
FROM
[dbo].websiteSnapshot
WHERE
websiteSnapshotStartTime < DATEADD(day, -1, GETDATE())
)) = snap.websiteSnapshotId
可以替换为
WHERE websiteSnapshotStartTime < DATEADD(day, -1, GETDATE())