felix
felix

Reputation: 11542

SQL Query with Multiple Like Clauses

I would like to create a SQL query, which does the following.. - I have a few parameters, for instance like "John","Smith" - Now I have a articles tables with a content column, which I would like to be searched - Now, How can I find out the rows in the articles table, which has the any one of those values("John","Smith")

I cannot use content LIKE "%john% or content LIKE "%smith%", as there could be any number of incoming parameters.

Can you guys please tell me a way to do this.... Thanks

Upvotes: 2

Views: 521

Answers (4)

onedaywhen
onedaywhen

Reputation: 57023

If full text search is overkill, consider putting the parameters in a table and use LIKE in theJOIN` condition e.g.

SELECT * -- column list in production code
  FROM Entities AS E1
       INNER JOIN Params AS P1
          ON E1.entity_name LIKE '%' + P1.param + '%';

Upvotes: 0

Jan
Jan

Reputation: 2293

This depends a lot on the DBMS you're using. Generally - if you don't want to use full-text search - you can almost always use regular expressions to achive this goal. For MySQL see this manual page - they even have example answering your question.

Upvotes: 0

Justin K
Justin K

Reputation: 2694

While HLGEM's solution is ideal, if full-text search is not possible, you could construct a regular expression that you could test only once per row. How exactly you do that depends on the DBMS you're using.

Upvotes: 1

HLGEM
HLGEM

Reputation: 96572

Have you considered full-text search?

Upvotes: 6

Related Questions