2011-10-13 117 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結尾。