1

Search the products with '^wall$', '\swall\s', '^wall ' or ' wall$' in its name. But it should not have results like 'wallpaper' or 'wonderwall'

SELECT * 
FROM `products` 
WHERE (products.name REGEXP 'wall?[:space]')  
ORDER BY products.updated_at DESC

So far the above obviously doesn't work. What should be the correct way to do this.

Updated the spec for clearer explanation.

2
  • Your spec is inconsistent. E.g. wallpaper matches products with "wall". Please can you be clearer about what you are trying to achieve? Are you attempting to match Any string where "wall" appears as a single word? Commented Apr 2, 2012 at 11:55
  • That's the problem that I am having. "wallpaper" should not be matched. It should be either nothing at the end or in front or just space in front or at the end. Commented Apr 2, 2012 at 11:58

3 Answers 3

5
SELECT * 
FROM `products` 
WHERE (products.name REGEXP '[[:<:]]wall[[:>:]]')  
ORDER BY products.updated_at DESC
Sign up to request clarification or add additional context in comments.

1 Comment

[[:<:]] and [[:>:]] are special metacharacters that match Start of word and End of word. There probably are also other special metacharacters that could have been used (like \b)
2

Can you try this REGEXP '[[:<:]]wall[[:>:]]'

Comments

2

use TRIM(products.name) = 'wall' instead of a regex...

after question update, use mysql equivalent of \bwall\b regex (word boundaries):

'[[:<:]]word[[:>:]]'

1 Comment

product name can be "wall flower" for example. this condition is not true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.