【问题标题】:Mysql REGEXP pattern SELECTMysql REGEXP 模式选择
【发布时间】:2014-03-28 17:44:46
【问题描述】:

我想实现以下,用伪mysql代码编写。

数据库表(protected_paths.tbl):

ID  Path_Protected

1   /home/folder/private

2   /home/folder2/another-private

3   /home/folder3/subfolder/another/private-folder

4   /home/folder4/my_protected_folder

输入字符串:

Test | Match | String

1 | Y | /home/folder/private/another (caught by ID 1 above)

2 | Y | /home/folder/private/another/more (also caught by ID 1 above)

3 | N | /home/folder/work (not matched in table above)

4 | N | /home/folder/another-private (not matched in table above)

5 | N | /home/folder3/subfolder (not matched in table above - table above, ID 3, refers to deeper path)

显然不行:

SELECT * FROM `protected_paths` WHERE Path_Protected = '/home/folder/private/another';

SELECT * FROM `protected_paths` WHERE Path_Protected REGEXP '/home/folder/private/another'; 

(我希望它与 protected_pa​​ths.tbl 中的 ID 1 匹配,因为它至少包含所有字符串)

任何帮助将不胜感激。 干杯,达里尔

【问题讨论】:

    标签: php mysql sql regex select


    【解决方案1】:

    不确定您是否需要REXEXP。您应该可以使用LIKECONCAT 来完成此操作:

    SELECT * 
    FROM protected_paths 
    WHERE '/home/folder/private/another' LIKE CONCAT('%',Path_Protected,'%')
    

    【讨论】:

    • 左边% 不是我想的OP!
    • @SabujHassan -- 不确定...... OP 声明 contains at least all of the string 将包括左侧......无论哪种方式都可以理解。谢谢!
    【解决方案2】:

    如果你有很多行,它不会很快,但这会起作用。

    SELECT COUNT(*)<>0 
      from tbl 
     where 'test-string' LIKE CONCAT(path_protected,'%')
    

    请看这里:http://sqlfiddle.com/#!2/d6023/3/0

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多