2015-10-30 17 views
1

我有一個表access_rules與列路徑。 我在表中添加一行:MySQL的正則表達式行爲

INSERT INTO access_rules (path) VALUES ('/user/search/*'), ('/user'), ('/someotherpath') 

現在,我執行查詢:

SELECT * FROM `access_rules` where '/user/search/ddd' regexp path 

我想回/用戶/搜索/ *,但查詢同時返回'/user/search/*''/user'。 有人可以告訴我爲什麼這樣嗎?

+0

我想如果你在'path'中有'/ search',它也會返回。 – hjpotter92

回答

0

/user^.*/useruser.*$相同。那就是任何都可以在/user之前或之後存在,以便進行匹配。

同時,/user/search/*匹配/user/search//////user/search。可能不是你想要的。您可能需要.*而不是*

既然你似乎只有「前綴」匹配,我建議你換到

WHERE '...' LIKE CONCAT(path, '%') 

如果/user被認爲是一個「精確」的比賽,那是行不通的。在這種情況下,切換到

INSERT INTO access_rules (path) 
    VALUES ('/user/search/%'), -- note the change here 
      ('/user'), 
      ('/someotherpath') 
SELECT * FROM `access_rules` where '/user/search/ddd' 
    LIKE path -- note LIKE, not REGEXP 
+0

警告:我沒有檢查該代碼;希望它會給你足夠的想法。 –

+0

其實SELECT * FROM'access_rules'其中'/ user/search/ddd'LIKE路徑是我想要的,謝謝 – Harikrishnan