2

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?

1
  • 2
    It depends on your need. It can be an AND, it can be an OR. Commented Nov 13, 2011 at 20:53

4 Answers 4

3

Have you tried:

SELECT * FROM hnurl WHERE title LIKE '%text1%' OR title LIKE '%text2%';
Sign up to request clarification or add additional context in comments.

1 Comment

@David19801: In MySQL || also works. See the documentation: dev.mysql.com/doc/refman/5.0/en/… Your problem must be something else.
2

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.

2 Comments

to use an external engine, would I have to put my entire database with them? What would hte benefit of full text search be over what I am doing here?
@David19801: The benefit? It's faster. Much, much faster.
0

You could add

... OR title LIKE '%text2%'

Comments

0

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.

Comments

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.