以下是现有的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'。我想在结果中保留这些记录。我很感激提出的任何想法。
答案 0 :(得分:2)
您的查询包含上下字符串比较目标的混合。据我所知,SQL Server默认不区分大小写;这可能是你的查询绊倒的原因吗?检查每个this answer的排序规则。
编辑:根据您更新的问题,你能不能只使用前面使用NOT的AND子句? 换句话说,添加一个'AND not(x)'子句,其中'x'是定义要排除的记录的条件?您需要嵌套客户测试,因为它是一个OR。 e.g:
... payhistory.comment NOT LIKE 'Elit%'
AND not ((customer like 'FTS%' or customer like 'EMC%') AND batchtype = 'PC')
作为旁注,我认为LIKE子句可能意味着某些(但not all)情况下的表格扫描效率低下,因此如果此查询将用于性能敏感的角色,您可能需要检查查询计划,并优化表格以适应。
答案 1 :(得分:2)
$sql="select * from builder_property where builder_pro_name LIKE '%%' OR builder_pro_name LIKE '%za%' AND status='Active'";
这将返回表格中将结束plaza
或complex
等名称的所有构建器属性名称。
答案 2 :(得分: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'
答案 3 :(得分:0)
将此条件添加到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')
希望对你有用。
答案 4 :(得分:0)
当构建复杂的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