Norse
Norse

Reputation: 5757

Wild card in REGEXP?

I have data like this in a longtext column:

Name: Adam Smith
Name: Bob Johnson
Name: John Hansen

Is it possible to use a wildcard, within regexp? Say a user searches Smith, in pseudo-code:

SELECT * FROM mytable
WHERE `file` REGEXP 'Name: %Smith%\n'

I'm pretty sure the % wildcard isn't a function within regexp. Is it possible to do this?

Upvotes: 2

Views: 3647

Answers (2)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324750

The REGEXP-using answer is:

SELECT * FROM mytable WHERE `file` REGEXP 'Name: .*Smith.*\n'

However LIKE is more suitable:

SELECT * FROM mytable WHERE `file` LIKE 'Name: %Smith%'

Upvotes: 5

mah
mah

Reputation: 39807

Your syntax is: SELECT * FROM mytable WHERE (columnName) LIKE 'Name: %Smith%';

Replace (columnName) as appropriate. Not clear if you meant longtext was the name of the column, but if it is, that's what you would replace it with.

Upvotes: 1

Related Questions