mysql - where 子句难题 : where "LIKE" one thing but not "LIKE another" is failing?

标签 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             

最佳答案

试试这个:

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

关于mysql - where 子句难题 : where "LIKE" one thing but not "LIKE another" is failing?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13493529/

相关文章:

mysql - 如何在 MySQL 的 instr() 中使用区分大小写?

java - 我坚持尝试返回字符串的某个部分,因为它是一个变量

ruby - 如何使用 ruby​​ 正则表达式获取子字符串?

mysql - 简单的 select, where 语句有问题

MySQL 左外连接多张表

php - 在 MySQL 中更新后不会回显消息

MySQL查询三张表

javascript - JavaScript 中的子字符串

MySQL Where 子句 - 获取两个城市,其中 1. 行的 ID < 2. 行的 ID

java - Hibernate 找不到 persistence.xml 文件