【问题标题】:SQL Checking for NULL and incrementalsSQL 检查 NULL 和增量
【发布时间】:2010-11-12 09:16:28
【问题描述】:

如果给定一个要检查的数字,我想检查是否有任何要返回的内容,如果该查询没有返回任何条目,请增加该数字直到达到一个条目并显示该条目。目前,代码如下所示:

        SELECT * 
    FROM news
    WHERE DATEDIFF(day, date, getdate() ) <= #url.d#
    ORDER BY date desc

#url.d# 是一个被传递的整数(比如 31)。如果没有返回任何结果,我想将 #url.d# 中存储的数字增加 1,直到找到一个条目。

【问题讨论】:

  • 如果您没有找到任何记录,您的意思是减少 #url.d# 吗?如果没有什么比给定值少,并且在提高该值时您不会找到任何项目。
  • 我的意思是检查今天日期和浏览器通过的日期(#url.date#)内的任何项目。默认显示过去 31 天,但说过去 40 天没有发布任何新闻,但我不想看到所有新闻报道甚至过去 60 天,而是我想自动发布单一的最新新闻文章。我希望澄清。

标签: sql null increment


【解决方案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

【讨论】:

  • 说得好,比我的更好更快!
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    首先,我认为您可能希望在 where 子句中使用 DateDiff 函数来 avpod,而不是计算所需的截止日期并在 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
    

    我还猜测您可能希望将日期四舍五入到午夜(我在这里没有这样做)。这是一种多查询方法,可能应该在单个存储过程中实现......如果这是您正在寻找的。​​p>

    祝项目顺利!

    【讨论】:

      【解决方案4】:

      如果你想要一行:

      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())
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多