2

I have a MySql database with some rows as follows:

ID    DESC
 1    This is my bike
 2    Motorbikes are great
 3    All bikers should wear helmets
 4    A bike is great for exercise
 5    A. Top. Bike.

What I want to do is return the rows with whitespace surrounding the search term, OR the term being at the end or beginning of the description.

For example,

"SELECT * FROM `mytable` WHERE `desc` LIKE '%bike%'"

Will return all rows. But,

"SELECT * FROM `mytable` WHERE `desc` LIKE '% bike %'

Will only return row 4.

What I really want is a reliable way to return rows 1, 4 and 5, i.e. where the search term is sorrounded with anything BUT chars A-z, 0-9. Any ideas? Is this even possible with MySql?

Thanks!!

3
  • you can use mysql regex Commented Feb 11, 2013 at 12:10
  • Regex in Mysql Query. Refer this Commented Feb 11, 2013 at 12:12
  • And the answer is... SELECT * FROM reviews WHERE description REGEXP '[[:<:]]desc[[:>:]]' Fast and efficient. Thanks StackOverflow!! Commented Feb 11, 2013 at 12:41

4 Answers 4

3

You can use regular expressions in SQL

SELECT * FROM `table` WHERE desc REGEXP '\bbike\b'
Sign up to request clarification or add additional context in comments.

Comments

1

You should start reading about MySql RegEx.

Sample Code.

SELECT * FROM table WHERE field_name REGEXP PATTERN;

More Specific

details Table

ID      NAME
1       Dipesh
2       Dip
3       Dipe
4       DiDi
5       Di
SELECT * FROM details WHERE NAME REGEXP '^Di$';

Result

NAME -> Di

SELECT * FROM details WHERE NAME REGEXP 'Di$';

Result

NAME -> DiDi , Di

SELECT * FROM details WHERE NAME REGEXP '^Di';

Result

NAME -> Dip, DiDi, Di

2 Comments

Thanks - Regex is the way to go.
From here: tech-recipes.com/rx/484/… SELECT * FROM desc WHERE description REGEXP '[[:<:]]bike[[:>:]]' Works well and returns results in 0.0075 seconds on my WAMP server with 25k+ rows
0

You need to specify the additional conditions in the query:

SELECT  *
FROM    `mytable`
WHERE
    `desc` LIKE '% bike %' OR
    `desc` LIKE '% bike' OR
    `desc` LIKE 'bike %';

6 Comments

LIKE is case-sensitive: force case comparison to ensure that row 5 is returned as well
@MarkBaker Interesting. According to MySQL Docs LIKE is case-insensitive.
Depends on collation; but you most recommendations these days are to use utf-8, which is case-sensitive
Agree, but default one isn't and in a lot of installations that's the case.
I think UTF8_collation is case insensitive, but UFT8_bin is sensitive according to my tests. One thing though - desc LIKE 'bike %' will still match 'motorbike', for example.
|
0

Try this one, hope it'll help you

"SELECT * FROM `mytable` WHERE `desc` LIKE '% bike'

1 Comment

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post.

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.