SQL LIKE % inside array

后端 未结 8 635
我寻月下人不归
我寻月下人不归 2020-12-05 04:52

I know how to perform an SQL LIKE % query for a single value like so:

SELECT * FROM users WHERE name LIKE %tom%;

but how do I do this if th

相关标签:
8条回答
  • 2020-12-05 05:45

    Many SQL systems let you push it as a character value. CHAR(10) = Linefeed. So, occasionally, instead of working out all the escape coding, you can push single quotes, double quotes and other things into a sql string this way, without too many other coding issues.

    -Just a hint.

    0 讨论(0)
  • 2020-12-05 05:46

    i just took the code of 472084.

    $sql = array('0'); // Stop errors when $words is empty
    
    foreach($words as $word){
        $sql[] = 'name LIKE %'.$word.'%'
    }
    
    $sql = 'SELECT * FROM users WHERE '.implode(" OR ", $sql);
    

    For my self, i had to modify it because it's returned me an error SQL. I Post it for people who gonna read the thread.

    foreach($words as $word){
        $sql[] = 'name LIKE \'%'.$word.'%\'';
    }
    
    $sql = 'SELECT * FROM users WHERE '.implode(" OR ", $sql);
    

    The difference between them is about quote, my mysql DB said there is a problem ! so i had to escape quote from $sql[] = 'name LIKE %'.$word.'%' and now it's work perfectly.

    0 讨论(0)
提交回复
热议问题