David19801
David19801

Reputation: 11448

mysql text search OR syntax

I have

SELECT * FROM hnurl WHERE title LIKE('%text1%')

How do I extend this to also add in things LIKE('%text2%')? within the same query results?

Upvotes: 2

Views: 89

Answers (4)

Oleksandr M.
Oleksandr M.

Reputation: 673

It is also possible with MySQL REGEXP, see the most simplified examples at http://dev.mysql.com/doc/refman/5.1/en/regexp.html, something like:

SELECT * FROM hnurl
WHERE title REGEXP 'text(1|2)'

Variations possible.

Upvotes: 0

phatfingers
phatfingers

Reputation: 10250

You could add

... OR title LIKE '%text2%'

Upvotes: 0

Mark Byers
Mark Byers

Reputation: 838256

Use OR if you want rows where either or both strings are found:

SELECT * FROM hnurl
WHERE title LIKE '%text1%'
OR title LIKE '%text2%'

Use AND if you want rows where both strings are found:

SELECT * FROM hnurl
WHERE title LIKE '%text1%'
AND title LIKE '%text2%'

Sidenote: You may also wish to consider using a full-text search or an external text search engine to improve the performance of your queries.

Upvotes: 2

Marcus
Marcus

Reputation: 12586

Have you tried:

SELECT * FROM hnurl WHERE title LIKE '%text1%' OR title LIKE '%text2%';

Upvotes: 3

Related Questions