user1143767
user1143767

Reputation: 1491

MYSQL - How to match a field with commas in it using like?

I'm trying to get a single record from my news feed. I pass it year, month and the news article title. I do the match based where the year = year and month = month and then I use like for the title:

LIKE '%title%'

this works in every case except when the title contains commas. Why doesn't this:

LIKE '%thank you donors volunteers and guests%'

match what's in the field: Thank You Donors, Volunteers and Guests

But if I put in the commas:

LIKE '%thank you donors, volunteers and guests%'

it matches it. How can I tell it to match the commas if I don't know the location of them?

Upvotes: 3

Views: 155

Answers (3)

CBusBus
CBusBus

Reputation: 2359

Use INSTR(), for example

SELECT field FROM table WHERE INSTR(field, "SEARCH STRING") > 0

INSTR() returns the starting index of a the first instance of the string that is to be matched starting with character index 1 so you can use it in a similar manner to LIKE without the function specific special character considerations.

Upvotes: -1

Jeff Wooden
Jeff Wooden

Reputation: 5479

I think you would be best served using boolean full text searches, you can customize your query to your parameters, I won't write the query for you but you can figure out how to here: http://dev.mysql.com/doc/refman/5.1/en/fulltext-boolean.html

Upvotes: 3

juergen d
juergen d

Reputation: 204766

It doesn't match because it is not the same string. % is a placeholder for any string but the rest of the content has to match exactly.

Upvotes: 2

Related Questions