首页 >数据库 >mysql教程 >如何在SQL Server 2008中正确使用WHERE子句中的CASE语句?

如何在SQL Server 2008中正确使用WHERE子句中的CASE语句?

Mary-Kate Olsen
Mary-Kate Olsen原创
2025-01-11 09:54:42747浏览

How Can I Correctly Use CASE Statements Within WHERE Clauses in SQL Server 2008?

解决 SQL Server 2008 WHERE 子句中的 CASE 语句问题

在 SQL Server 2008 的 WHERE 子句中使用 CASE 语句可能会带来困难。 以下查询举例说明了一个常见问题:

<code class="language-sql">SELECT
    tl.storenum 'Store #', 
    co.ccnum 'FuelFirst Card #', 
    co.dtentered 'Date Entered',
    CASE st.reasonid 
        WHEN 1 THEN 'Active' 
        WHEN 2 THEN 'Not Active' 
        WHEN 0 THEN st.ccstatustypename 
        ELSE 'Unknown' 
    END 'Status',
    CASE st.ccstatustypename 
        WHEN 'Active' THEN ' ' 
        WHEN 'Not Active' THEN ' ' 
        ELSE st.ccstatustypename 
    END 'Reason',
    UPPER(REPLACE(REPLACE(co.personentered,'RT\\',''),'RACETRAC\\','')) 'Person Entered',
    co.comments 'Comments or Notes'
FROM 
    comments co
    INNER JOIN cards cc ON co.ccnum=cc.ccnum
    INNER JOIN customerinfo ci ON cc.customerinfoid=ci.customerinfoid
    INNER JOIN ccstatustype st ON st.ccstatustypeid=cc.ccstatustypeid
    INNER JOIN customerstatus cs ON cs.customerstatuscd=ci.customerstatuscd
    INNER JOIN transactionlog tl ON tl.transactionlogid=co.transactionlogid
    LEFT JOIN stores s ON s.StoreNum = tl.StoreNum
WHERE 
    CASE LEN('TestPerson')
        WHEN 0 THEN co.personentered  = co.personentered
        ELSE co.personentered LIKE '%TestPerson'
    END 
    AND cc.ccnum = CASE LEN('TestFFNum')
        WHEN 0 THEN cc.ccnum 
        ELSE 'TestFFNum' 
    END 
    AND CASE LEN('2011-01-09 11:56:29.327') 
        WHEN 0 THEN co.DTEntered = co.DTEntered 
        ELSE 
            CASE LEN('2012-01-09 11:56:29.327') 
                WHEN 0 THEN co.DTEntered >= '2011-01-09 11:56:29.327' 
                ELSE co.DTEntered BETWEEN '2011-01-09 11:56:29.327' AND '2012-01-09 11:56:29.327' 
            END 
    END
    AND tl.storenum < 699 
ORDER BY tl.StoreNum</code>

解决方案:用布尔逻辑重构

核心问题在于 WHERE 子句的条件逻辑中直接使用了 CASE 语句。 CASE 表达式应该产生一个值;它们不应该是全部情况。

解决方案涉及使用 ANDOR 运算符将 CASE 语句替换为等效的布尔逻辑:

<code class="language-sql">SELECT
    tl.storenum 'Store #', 
    co.ccnum 'FuelFirst Card #', 
    co.dtentered 'Date Entered',
    CASE st.reasonid 
        WHEN 1 THEN 'Active' 
        WHEN 2 THEN 'Not Active' 
        WHEN 0 THEN st.ccstatustypename 
        ELSE 'Unknown' 
    END 'Status',
    CASE st.ccstatustypename 
        WHEN 'Active' THEN ' ' 
        WHEN 'Not Active' THEN ' ' 
        ELSE st.ccstatustypename 
    END 'Reason',
    UPPER(REPLACE(REPLACE(co.personentered,'RT\\',''),'RACETRAC\\','')) 'Person Entered',
    co.comments 'Comments or Notes'
FROM 
    comments co
    INNER JOIN cards cc ON co.ccnum=cc.ccnum
    INNER JOIN customerinfo ci ON cc.customerinfoid=ci.customerinfoid
    INNER JOIN ccstatustype st ON st.ccstatustypeid=cc.ccstatustypeid
    INNER JOIN customerstatus cs ON cs.customerstatuscd=ci.customerstatuscd
    INNER JOIN transactionlog tl ON tl.transactionlogid=co.transactionlogid
    LEFT JOIN stores s ON s.StoreNum = tl.StoreNum
WHERE 
    (LEN('TestPerson') = 0 AND co.personentered = co.personentered) OR (LEN('TestPerson') > 0 AND co.personentered LIKE '%TestPerson')
    AND ((LEN('TestFFNum') = 0 AND cc.ccnum = cc.ccnum) OR (LEN('TestFFNum') > 0 AND cc.ccnum = 'TestFFNum'))
    AND ((LEN('2011-01-09 11:56:29.327') = 0 AND co.DTEntered = co.DTEntered) OR (LEN('2012-01-09 11:56:29.327') = 0 AND co.DTEntered >= '2011-01-09 11:56:29.327') OR (co.DTEntered BETWEEN '2011-01-09 11:56:29.327' AND '2012-01-09 11:56:29.327'))
    AND tl.storenum < 699
ORDER BY tl.StoreNum;</code>

性能增强

虽然这个更正后的查询可以正确运行,但性能还可以进一步提高:

  • 减少嵌套:深度嵌套的OR条件可能会降低效率。 考虑替代查询结构(例如,使用 UNION ALL 来表示不同的搜索条件)。
  • 索引: 确保 WHERE 子句中经常使用的列上存在索引(例如 co.personenteredcc.ccnumco.DTEnteredtl.storenum)。
  • 参数化:如果这些“测试”值是变量,请使用参数化查询来避免重复查询解析。

通过解决这些问题,您可以创建更高效​​、更易读的 SQL 查询。

以上是如何在SQL Server 2008中正确使用WHERE子句中的CASE语句?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn