简体   繁体   中英

SQL: How can I determine quickly whether a string (sentence) includes words from my sql database?

I have a question/problem. How can I determine quickly whether a string (sentence) includes words from my sql database? In PHP I use following code:

if(strpos($string,"abc")!==false) { 
    //abc is in $string 
} else {
    //abc isn't in $string 
}

Example:

Sql-Database: words -> my garden, sun, water

String: My garden is green.

Has someone suggestions? Thank you.

so let's assume that you fetched the data from the database using a while loop

 $senetence = "The man's garden was green";
 $wanted_words = 'garden'; 
 if($wanted_word = stripos($senetence, $wanted_words))
 {
    echo "<pre>";
    print_r($wanted_words .": is at position ". $wanted_word ." from ". $senetence);
    echo "</pre>";
    return false;
 }
 else
 {
    return true;
 }

You can use a query like this:

SELECT
  *
FROM
  some_table
WHERE
  'My garden is green' LIKE BINARY CONCAT('%', some_column, '%')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM