Terence
Terence

Reputation: 11088

SQL query for words (not the sentence)

I would like to query a single column (varchar):

sample datarows:

1) The fox jumps like a foo on my bar
2) Jumpers are not cool
3) Apple introduced iJump

When I enter a search criteria like... jump

I expect to get a resultset of: jumps, Jumpers, iJump (So I dont want the complete row)

Currently I'm using MySQL (I'm open to suggestions as long it's open source)

Upvotes: 1

Views: 269

Answers (2)

Michael Fredrickson
Michael Fredrickson

Reputation: 37388

Since you're using MySQL, I might suggest looking into LIB_MYSQLUDF_PREG.

This open source library will provide you with additional regex functionality, including the PREG_CAPTURE function, which extracts a regex match from a string.

Using this function, you could easily build a regex to return the match you're looking for... Something like:

\b\w*jump\w*\b

Upvotes: 2

Brendan Long
Brendan Long

Reputation: 54252

Getting any row with your search criteria is easy:

SELECT sentence
FROM sentences
WHERE sentence LIKE '%jump%'

I'd probably do the rest in application logic, since doing it in the database doesn't help you at all.

Also, any method of splitting a string and handling it will probably be database-specific, so you would need to say which one you're using.

Upvotes: 1

Related Questions