where子句难题:哪里“喜欢”一件事而不是“LIKE另一件”失败了?

时间:2012-11-21 12:42:33

标签: mysql substring where-clause

嗨我有一个表结构:

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             

1 个答案:

答案 0 :(得分:1)

试试这个:

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