2012-11-21 54 views
0

您好我有這樣一個表結構:Where clause conundrum:where「LIKE」one thing but not「like another」is failing?

mysql>DESC file; 
file_id   
fullpathname 

與記錄

file_id | fullpathname           
--------+------------------------------------------------------ 
     6 | /var/tmp/4x4 and 6x4/test/test3.txt 
     7 | /var/tmp/4x4 and 6x4/test/somefile 
     8 | /var/tmp/4x4 and 6x4/test   
     9 | /var/tmp/4x4 and 6x4/6x4_1.JPG  
    10 | /var/tmp/4x4 and 6x4/6x4_2.JPG  
    11 | /var/tmp/4x4 and 6x4/4x4_2.jpg  
    12 | /var/tmp/4x4 and 6x4/4x4_1.jpg  
    13 | /var/tmp/dir/4x4 and 6x4    

和查詢需要是差不多就是下面我有(但它不工作)

SELECT * FROM file 
WHERE fullpathname LIKE '/var/tmp/4x4 and 6x4%' 
AND fullpathname NOT LIKE '/var/tmp/4x4 and 6x4/%/' 

,但它不需要返回它需要的file_id 6和7,只需要選擇一個節點深的「路徑」。這是一個奇怪的人,但這樣的情況

它應該返回的是

file_id | fullpathname           
--------+------------------------------------------------------ 
     8 | /var/tmp/4x4 and 6x4/test   
     9 | /var/tmp/4x4 and 6x4/6x4_1.JPG  
    10 | /var/tmp/4x4 and 6x4/6x4_2.JPG  
    11 | /var/tmp/4x4 and 6x4/4x4_2.jpg  
    12 | /var/tmp/4x4 and 6x4/4x4_1.jpg  
    13 | /var/tmp/dir/4x4 and 6x4    
+0

'13 |/var/tmp/dir/4x4和6x4'不會被返回,因爲它不是'LIKE'/ var/tmp/4x4和6x4%'' – StuartLC

+0

不可以,沒關係那是我忘記識別的另一個遺漏,這對我的需求沒問題,我不想要'13''6'或'7' – conners

回答

1

試試這個:

SELECT * FROM file 
WHERE fullpathname LIKE '/var/tmp/4x4 and 6x4%' 
AND fullpathname NOT LIKE '/var/tmp/4x4 and 6x4/%/%' 
+0

是的,你說得對,當然,因爲隨後的字符串,當然 – conners