【问题标题】:where clause conundrum: where "LIKE" one thing but not "LIKE another" is failing?where 子句难题:在哪里“喜欢”一件事而不是“喜欢另一件事”失败了?
【发布时间】:2012-11-21 12:42:33
【问题描述】:

嗨,我有一个表结构:

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             

【问题讨论】:

  • 13 | /var/tmp/dir/4x4 and 6x4 不会被返回,因为它不是LIKE '/var/tmp/4x4 and 6x4%'
  • 不,没关系,这是我忘记指出的额外遗漏,这对我不想要13 67 的需求来说是可以的

标签: mysql substring where-clause


【解决方案1】:

试试这个:

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

【讨论】:

  • 是的,你说得对,当然,因为后面的字符串,当然
猜你喜欢
  • 1970-01-01
  • 2021-01-27
  • 1970-01-01
  • 1970-01-01
  • 2020-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-18
相关资源
最近更新 更多