【问题标题】:select query using multiple like conditions使用多个类似条件选择查询
【发布时间】:2011-12-07 04:19:24
【问题描述】:

以下是现有的 ms sql server 2008 报告查询。

SELECT
    number, batchtype, customer, systemmonth, systemyear, entered, comment, totalpaid
FROM
    payhistory LEFT OUTER JOIN agency ON
        payhistory.SendingID = agency.agencyid      
WHERE
    payhistory.batchtype LIKE 'p%' AND
    payhistory.entered >= '2011-08-01 00:00:00.00' AND
    payhistory.entered <  '2011-08-15 00:00:00.00' AND
    payhistory.systemmonth = 8 AND
    payhistory.systemyear = 2011 AND
    payhistory.comment NOT LIKE 'Elit%'

结果将如下所示:

number  batchtype   customer    systemmonth systemyear  entered     comment         totalpaid
6255756 PC      EMC1106     8       2011        12:00:00 AM DP From - NO CASH       33
5575317 PA      ERS002      8       2011        12:00:00 AM MO-0051381526 7/31      20
6227031 PA      FTS1104     8       2011        12:00:00 AM MO-10422682168 7/30     25
6232589 PC      FTS1104     8       2011        12:00:00 AM DP From - NO CASH       103
2548281 PC      WAP1001     8       2011        12:00:00 AM NCO DP $1,445.41        89.41
4544785 PCR     WAP1001     8       2011        12:00:00 AM NCO DP $1,445.41        39

我要做的是修改查询,以排除客户为“FTS%”和“EMC%”以及batchtype =“PC”的记录。正如您在结果集中看到的那样,有一些记录,其中客户类似于 FTS% 和 batchtype = 'PA'。我想在结果中保留这些记录。我将不胜感激提供的任何想法。

【问题讨论】:

  • 您得到了哪些不良结果?
  • 那么,为什么要说“仅当 btype = 'PC'”然后输入btype &lt;&gt; 'PC'?
  • 您的 SQL 有一个额外的括号,或者它缺少一个...

标签: sql sql-server sql-server-2008 sql-like


【解决方案1】:

您的查询包含上下字符串比较目标的混合。据我所知,SQL Server 默认情况下不区分大小写。这可能是您的查询绊倒的原因吗?根据this answer 检查排序规则。

编辑:根据您更新的问题,您能否仅使用在前面使用 NOT 的 AND 子句? 换句话说,添加一个“AND not (x)”子句,其中“x”是定义要排除的记录的条件?您需要嵌套客户测试,因为它是 OR。 例如:

... payhistory.comment NOT LIKE 'Elit%'
AND not ((customer like 'FTS%' or customer like 'EMC%') AND batchtype = 'PC')

附带说明,我相信 LIKE 子句在某些情况下(但 not all)可能意味着低效的表扫描,因此如果此查询将用于对性能敏感的角色,您可能需要检查查询计划并优化表格以适合。

【讨论】:

  • 是的!这正是我需要做的。我花了太多时间试图把那个语句放在一起,不知道我可以使用“NOT”参数来过滤掉这些记录。谢谢大家!!
  • 太棒了!另外-如果它回答了您的问题,您可以将其标记为答案吗?
【解决方案2】:
$sql="select * from builder_property where builder_pro_name LIKE '%%' OR builder_pro_name LIKE '%za%' AND status='Active'";

这将返回表中所有以 plaza 或 complex 结尾的构建器属性名称。

【讨论】:

    【解决方案3】:

    这可能是因为您的服务器可能区分大小写。在这种情况下,下面的查询会起作用。

    SELECT
    table1.number, table1.btype, table1.cust, table1.comment, table2.ACode
    FROM
    table1 LEFT OUTER JOIN table2 ON table1.1ID = table2.2ID
    WHERE
    lower(table1.btype) LIKE 'p%' AND
    lower(table1.comment) NOT LIKE 'yyy%' AND
    lower(table1.cust) NOT LIKE 'abc%' AND
    lower(table1.cust) NOT LIKE 'xyz%' AND
    lower(table1.btype) <> 'pc'
    

    【讨论】:

      【解决方案4】:

      将此条件添加到 WHERE 子句中:

      NOT((customer LIKE 'FTS%' OR customer LIKE 'EMC%') AND batchtype='PC')
      

      假设您的其他结果正常并且您只想过滤掉它们,那么整个查询将是

      SELECT
          number, batchtype, customer, systemmonth, systemyear, entered, comment, totalpaid
      FROM
          payhistory 
          LEFT OUTER JOIN agency ON
              payhistory.SendingID = agency.agencyid      
      WHERE
          payhistory.batchtype LIKE 'p%' AND
          payhistory.entered >= '2011-08-01 00:00:00.00' AND
          payhistory.entered <  '2011-08-15 00:00:00.00' AND
          payhistory.systemmonth = 8 AND
          payhistory.systemyear = 2011 AND
          payhistory.comment NOT LIKE 'Elit%' AND
          NOT((payhistory.customer LIKE 'FTS%' OR payhistory.customer LIKE 'EMC%') AND payhistory.batchtype='PC')
      

      希望对你有用。

      【讨论】:

        【解决方案5】:

        在构建复杂的 where 子句时,最好使用括号来保持一切正常。此外,当使用多个 NOT LIKE 语句时,您必须使用 ORs 将所有 NOT LIKE 条件组合在一起,并将它们包装在单独的 中>AND这样的条件...

        
        WHERE     
            (payhistory.batchtype LIKE 'p%')
        AND (payhistory.entered >= '2011-08-01 00:00:00.00')
        AND (payhistory.entered <  '2011-08-15 00:00:00.00')
        AND (payhistory.systemmonth = 8 )
        AND (payhistory.systemyear = 2011)
        AND ( // BEGIN NOT LIKE CODE
        
            (payhistory.comment NOT LIKE 'Elit%') 
        
        OR  (
            (payhistory.customer NOT LIKE 'EMC%') AND 
            (payhistory.batchtype = 'PC')
            ) 
        
        OR  (
            (payhistory.customer NOT LIKE 'FTS%') AND 
            (payhistory.batchtype = 'PC')
            )
        
            ) //END NOT LIKE CODE
        

        【讨论】:

          猜你喜欢
          • 2018-09-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-21
          • 2015-12-29
          相关资源
          最近更新 更多