我想检查给定一个要检查的数字是否有任何东西要返回,如果该查询没有返回条目,请增加数字直到达到一个条目并显示该条目。目前,代码如下所示:
SELECT *
FROM news
WHERE DATEDIFF(day, date, getdate() ) <= #url.d#
ORDER BY date desc
其中#url.d# 是一个被传递的整数(比如31)。如果没有返回任何结果,我想将 #url.d# 中存储的数字增加 1,直到找到一个条目。
这种增量查询效率不高。通过说 - “我永远不需要超过 100 个结果,所以给我这些”,你会得到更好的结果:
SELECT top 100 *
FROM news
ORDER BY date desc
如果您只需要特定日期的项目(例如具有共同日期的项目作为结果中的第一项),则在客户端进一步过滤。
或者,您可以将多个查询请求转换为两个查询请求:
DECLARE
@theDate datetime,
@theDate2 datetime
SET @theDate = (SELECT Max(date) FROM news)
--trim the time off of @theDate
SET @theDate = DateAdd(dd, DateDiff(dd, 0, @theDate), 0)
SET @theDate2 = DateAdd(dd, 1, @theDate)
SELECT *
FROM news
WHERE @theDate <= date AND date < @theDate2
ORDER BY date desc
在MySQL:
SELECT news.*,
(
SELECT COUNT(*)
FROM news
WHERE date < DATEADD(day, GETDATE(), -#url.d#)
)
FROM news
WHERE date >= DATEADD(day, GETDATE(), -#url.d#)
ORDER BY
date DESC
LIMIT 1
在SQL Server:
SELECT TOP 1
news.*,
(
SELECT COUNT(*)
FROM news
WHERE date < DATEADD(day, GETDATE(), -#url.d#)
)
FROM news
WHERE date >= DATEADD(day, GETDATE(), -#url.d#)
ORDER BY
date DESC
请注意,使用此语法会使您的查询sargable,即可以使用索引进行date有效过滤。
首先,我认为您可能希望在 where 子句中使用 DateDiff 函数,而不是计算所需的截止日期并在 where 子句中的日期列上使用任何计算,这将更有效,所以而不是
WHERE DATEDIFF(day, date, getdate() ) <= #url.d#
你会有类似的东西
WHERE date >= @cutoffDate
其中@cutoffDate 是基于#url.d# 的计算日期
现在,至于抓住正确的截止日期。我的假设是,在正常情况下,请求会返回文章,否则您将只获取最近日期的文章。因此,我将采取的方法是获取计算截止日期的最旧日期(基于#url.d# 和最近的文章日期。类似的东西
-- @urld == #url.d
-- compute the cutoff date as the OLDEST of the most recent article and
-- the date based on #url.d
declare @cutoff datetime
select @cutoff = DateAdd(dd,-1*@urld,GetDate())
select @cutoff
select @cutoff = min(cutoffDate)
from
(SELECT Max(date) as cutoffDate from News
UNION
select @cutoff) Cutoff
-- grab the articles with dates that are more recent than the cutoff date
select *
from News
WHERE date >= @cutoff
我还猜想您可能希望将日期四舍五入到午夜(我在这里没有这样做)。这是一种多查询方法,可能应该在单个存储过程中实现......如果这是您正在寻找的。
祝项目顺利!
如果你想要一行:
SELECT t.*
FROM NEWS t
WHERE t.id = (SELECT MAX(n.id)
FROM NEWS n
WHERE n.date BETWEEN DATEADD(day, -:url.d, getDate()) AND getDate())
DATEADD 使用负数来返回所需的天数可能并不明显。
如果您想要该日期的所有行:
SELECT t.*
FROM NEWS t
WHERE t.date BETWEEN DATEADD(day, -:url.d, getDate()) AND getDate())