1

I have a Table schema.

Id, Name, Email, Description.

Description column will have a lot of text for each row. Now I want to perform a search in that column based on the words that user have entered. Let say the user have entered five words for search.

  1. Busy
  2. Good
  3. Nature
  4. Block
  5. Sweet

Now I want to perform a search that if any row of description column matches any of at least three of the above five, it should give that row in the result set. I don't do not want all words match or a single match. I want at least three words to be matched in each row of Description column. I know about LIKE Statement But how can I make it so that I can get my desired Result. Ignoring Case sensitivity. How Can I do that? Can Anybody help me to make SQL query for my problem?

Shahid Iqbal
  • 2,059
  • 2
  • 21
  • 29

2 Answers2

1

I'm sure there should be a lot of better solutions but I found funny this problem and here's a proposal

SELECT IF ( LENGTH(CONCAT(
    IF (Description LIKE '%busy%', '1', ''), 
    IF (Description LIKE '%good%', '1', ''), 
    IF (Description LIKE '%nature%', '1', ''), 
    IF (Description LIKE '%blck%', '1', ''), 
    IF (Description LIKE '%sweet%', '1', '')        
)) > 2, 'Matches', 'Does not match') FROM ....

XDD I haven't thought in efficiency at all

Olvathar
  • 551
  • 3
  • 10
1

Olvathar's idea is right but it will only show the Matches or Does not match results. However, if you want to get the records against this control you should use if statement inside the where clause to show only the records where condition is true.

SELECT *  FROM TABLENAME WHERE Description IS NOT NULL AND IF ( LENGTH(CONCAT(
IF (Description LIKE '%busy%', '1', ''), 
IF (Description LIKE '%good%', '1', ''), 
IF (Description LIKE '%nature%', '1', ''), 
IF (Description LIKE '%blck%', '1', ''),
IF (Description LIKE '%sweet%', '1', '')
)) > 2, 1, 0)=1

Hope this will help.

Shahid Iqbal
  • 2,059
  • 2
  • 21
  • 29