2011-10-13 116 views
2

下面是现有的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'。我想在结果中保留这些记录。我希望提供任何想法。

+2

你得到的不良结果是什么? – CamelSlack

+0

那么,为什么你说“只有当btype ='PC'”,然后把'btype <>'PC''? – Lamak

+0

你的SQL有一个额外的括号,或者它缺少一个... – Matthew

回答

2

您的查询包含上下字符串比较目标的混合。据我所知,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)情况下的低效表扫描,因此,如果该查询将在您可能会在性能敏感的角色使用要检查查询计划,并优化表以适应。

+0

是的!这正是我需要做的。我花了太多时间试图将这个陈述放在一起,不知道我可以使用“NOT”参数来过滤这些记录。谢谢大家!! – user992322

+0

太棒了!另外 - 如果它回答你的问题,你能将它标记为答案吗? – Geoff

0

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

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' 
0

添加这个条件的WHERE子句:

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

假设你的其他结果都OK,你只是想筛选那些出,整个查询将

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

希望这对你有用。

0

在构建复合体的条款时,使用括号保持一切正常是个不错的主意。同时使用多个当NOT LIKE语句你必须将所有的使用手术室 NOT LIKE条件在一起,敷它们单独条件像这里面......


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 
2
$sql="select * from builder_property where builder_pro_name LIKE '%%' OR builder_pro_name LIKE '%za%' AND status='Active'"; 

这将返回表格中的所有生成器属性名称,名称将以plazacomplex结尾。