2013-05-13 85 views
1

我需要一些使用LIKE和NOT LIKE的幫助...我有一個查詢,我通過WHERE子句通過基於我的請求變量來自另一個服務器。其中一個查詢如下所示:MySQL和PHP - 使用'LIKE'和'NOT LIKE'

'CONNECT' => 
    "(detail_head.comment LIKE '%port%' 
     or detail_head.comment LIKE '%forward%' 
     or detail_head.comment LIKE '%connect%' 
     or detail_head.comment LIKE '%router%' 
     or detail_head.comment LIKE '%fire%wall%' 
     or detail_head.comment LIKE '%sonic%' 
    ) AND (
     detail_head.comment NOT LIKE '%report%' 
     OR detail_head.comment NOT LIKE '%portal%' 
    )", 

您可以看到我使用了LIKE而不是LIKE。不幸的是,這種方式並不按照我希望的方式工作。我猜這是因爲我要求PORT,但不是REPORT,所以無論如何它都給了我LIKE。

我想知道在這種情況下我應該做些什麼。我正在考慮將另一個查詢或數組作爲「排除列表」使用。如果查詢是LIKE語句,我可以在我的WHERE子句中使用'table_uid NOT IN(COMMA SEPARATED UID列表)'。

我有我的LIKE語句,我想排除:

$exclude_where_clauses = array(
     'CC'   => "(detail_head.comment LIKE '%ccb%') ", 
     'CONNECT'  => "(detail_head.comment LIKE '%report%' OR detail_head.comment LIKE '%portal%') ", 
     'EO'   => "(detail_head.comment LIKE '%OCU%' AND detail_head.comment LIKE '%KS%' AND detail_head.comment LIKE '%screen%' AND detail_head.comment LIKE '%term%') ", 
     'INVENTORY'  => "(detail_head.comment LIKE '%discount%') ", 
     'KS'   => "(detail_head.comment LIKE '%panel%' or detail_head.comment LIKE '%PMIX%' or detail_head.comment LIKE '%pmix%') ", 
     'OCUS'   => "(detail_head.comment LIKE '%document%') ", 
     'SALES'   => "(detail_head.comment LIKE '%point%') ", 
     'SECURITY'  => "(detail_head.comment LIKE '%km%') ", 
     'TERMS'   => "(detail_head.comment LIKE '%forward%' or detail_head.comment LIKE '%sales%' or detail_head.comment LIKE '%intermittent%' or detail_head.comment LIKE '%print%' or detail_head.comment LIKE '%de%min%' or detail_head.comment LIKE '%reciept%' or detail_head.comment LIKE '%time%') ", 
); 

所以,到最後,我想轉產我目前的查詢陣列說"(detail_head.comment LIKE '%port%' or detail_head.comment LIKE '%forward%' or detail_head.comment LIKE '%connect%' or detail_head.comment LIKE '%router%' or detail_head.comment LIKE '%fire%wall%' or detail_head.comment LIKE '%sonic%') AND table_uid NOT IN(LIST OF COMMA SEPARATED UIDs) "

+0

請張貼一個例子評論,也一樣,所以我們可以看到在上下文中的「報告」和「端口」。 – 2013-05-13 13:35:43

+0

另外請注意,我認爲我最想做的事情是編碼出來......我只是不確定它會如何工作: – brandoncluff 2013-05-13 13:36:30

+0

$ exclude_where_clause = $ exclude_where_clauses [$ _ REQUEST ['count_request']]; $ not_like_qry =「SELECT detail_head.scr FROM detail_head WHERE detail_head.call_status!='c'AND call_origins_uid!= 5 AND $ exclude_where_clause」; $ qry。=「AND call_origins_uid!= 5 GROUP BY detail_head.scr」; $ dob-> setQuery($ not_like_qry); ($ dob-> fetch(MYSQL_ASSOC)){ $ rows [] = $ dob-> getRowAsArray(); } foreach($ rows as $ row){ fputcsv($ tmpdata,$ row,'|'); } – brandoncluff 2013-05-13 13:37:51

回答

1

試試這個:

'CONNECT' => " 
    ( detail_head.comment LIKE '%port%' 
    OR detail_head.comment LIKE '%forward%' 
    OR detail_head.comment LIKE '%connect%' 
    OR detail_head.comment LIKE '%router%' 
    OR detail_head.comment LIKE '%fire%wall%' 
    OR detail_head.comment LIKE '%sonic%' 
    ) 
    AND NOT (
      detail_head.comment LIKE '%ccb%' 
     OR detail_head.comment LIKE '%report%' 
     OR detail_head.comment LIKE '%portal%' 
     OR detail_head.comment LIKE '%OCU%' 
     OR detail_head.comment LIKE '%KS%' 
     OR detail_head.comment LIKE '%screen%' 
     OR detail_head.comment LIKE '%term%' 
     OR detail_head.comment LIKE '%discount%' 
     OR detail_head.comment LIKE '%panel%' 
     OR detail_head.comment LIKE '%PMIX%' 
     OR detail_head.comment LIKE '%pmix%' 
     OR detail_head.comment LIKE '%document%' 
     OR detail_head.comment LIKE '%point%' 
     OR detail_head.comment LIKE '%km%' 
     OR detail_head.comment LIKE '%forward%' 
     OR detail_head.comment LIKE '%sales%' 
     OR detail_head.comment LIKE '%intermittent%' 
     OR detail_head.comment LIKE '%print%' 
     OR detail_head.comment LIKE '%de%min%' 
     OR detail_head.comment LIKE '%reciept%' 
     OR detail_head.comment LIKE '%time%' 
    ) 
", 
0

我不要相信這會更有效率(效率可能確實較低),但它可能是一種更合理地定義規格的方法 - 使用regexp()中的單詞邊界。

這將返回0:

SELECT 'foo report bar' REGEXP '[[:<:]]port[[:>:]]'; 

如果這返回1:

SELECT 'foo report bar' REGEXP '[[:<:]]report[[:>:]]'; 

MySQL has more info in the manual。 (5.1手動鏈接)

根據手頭任務的性質,以及這將對我的數據庫服務器施加多少壓力和多少壓力,我可能會考慮添加字段或相關表以幫助我處理(當我插入數據時),所以我可以在事後以更輕的方式運行這樣的報告 - 而不必在全文字段中進行繁重的文本處理。