views:

53

answers:

4

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

+5  A: 

Have you considered full-text search?

HLGEM
Wow, not sure why this got downvoted... bumping back up.
Justin K
This is why I commented above about "which database" -- I knew someone would answer this way.
MJB
+1  A: 

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.

Justin K
A: 

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.

Kaltas
A: 

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 + '%';
onedaywhen