2014-03-28 63 views
0

我想实现下面的,用伪mysql代码写的。Mysql REGEXP模式SELECT

数据库表(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'; 

(我想这在匹配ID 1 protected_pa​​ths.tbl,因为它至少包含所有的字符串)

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

回答

0

不知道你需要为这个或不REXEXP。你应该能够LIKECONCAT来实现:

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

左侧''%是不是OP想我猜! –

+0

@SabujHassan - 不确定... OP状态'至少包含所有包含左侧的字符串......通过任一方式获取点。谢谢! – sgeddes

0

如果你有很多的行,这不会很快,但这将工作。

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

在这里看到:如果http://sqlfiddle.com/#!2/d6023/3/0