Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用SQL在两个字段中查找字符串_Php_Sql - Fatal编程技术网

Php 使用SQL在两个字段中查找字符串

Php 使用SQL在两个字段中查找字符串,php,sql,Php,Sql,我正在开发一个搜索模块,就像你寻找朋友时的facebook 因此,我的表user中有两个字段:firstname和name 例如,如果我有一个用户:firstname:Georges和name:Clooney,我想,当我写: 地理 克洛 卢格奥尔 乔治尼 等等 检索此用户 如何使用SQL实现这一点?我想写一个非常宽松的搜索模块 SELECT * FROM User WHERE firstname LIKE '%'+@input+'%' OR name LIKE '%'+@input+'%'

我正在开发一个搜索模块,就像你寻找朋友时的facebook

因此,我的表user中有两个字段:
firstname
name

例如,如果我有一个用户:
firstname:Georges
name:Clooney
,我想,当我写:

  • 地理
  • 克洛
  • 卢格奥尔
  • 乔治尼
  • 等等
检索此用户

如何使用SQL实现这一点?我想写一个非常宽松的搜索模块

SELECT *
FROM User
WHERE firstname LIKE '%'+@input+'%' OR name LIKE '%'+@input+'%'
其中,
@input
是文本框中插入的文本

其中,
@input
是文本框中插入的文本

其中,
@input
是文本框中插入的文本

其中,
@input
是文本框中插入的文本。

尝试以下查询:

   SELECT * from user 
   WHERE name LIKE '%search_string%'
   OR firstname LIKE '%search_string%'
它将匹配任何名字或包含搜索字符串的名字。它将显示名称或名字中包含此字符串的所有用户。

尝试此查询:

   SELECT * from user 
   WHERE name LIKE '%search_string%'
   OR firstname LIKE '%search_string%'
它将匹配任何名字或包含搜索字符串的名字。它将显示名称或名字中包含此字符串的所有用户。

尝试此查询:

   SELECT * from user 
   WHERE name LIKE '%search_string%'
   OR firstname LIKE '%search_string%'
它将匹配任何名字或包含搜索字符串的名字。它将显示名称或名字中包含此字符串的所有用户。

尝试此查询:

   SELECT * from user 
   WHERE name LIKE '%search_string%'
   OR firstname LIKE '%search_string%'

它将匹配任何名字或包含搜索字符串的名字。它将显示所有名字或名字中包含此字符串的用户。

您需要的是这个小朋友:
%

其中firstname类似“%”$name.“%”或name类似“%”$name.“%”

以下是一个教程:


你需要的是这个小朋友:
%

其中firstname类似“%”$name.“%”或name类似“%”$name.“%”

以下是一个教程:


你需要的是这个小朋友:
%

其中firstname类似“%”$name.“%”或name类似“%”$name.“%”

以下是一个教程:


你需要的是这个小朋友:
%

其中firstname类似“%”$name.“%”或name类似“%”$name.“%”

以下是一个教程:

同时选中两个字段并尝试匹配

同时选中两个字段并尝试匹配

同时选中两个字段并尝试匹配

同时选中两个字段并尝试匹配。

尝试以下操作:

Select * from User where `name` LIKE '%search_string%' OR `firstname`
 LIKE '%search_string%' OR CONCAT(firstname,' ',name) LIKE '%search_string'%'
试试这个:

Select * from User where `name` LIKE '%search_string%' OR `firstname`
 LIKE '%search_string%' OR CONCAT(firstname,' ',name) LIKE '%search_string'%'
试试这个:

Select * from User where `name` LIKE '%search_string%' OR `firstname`
 LIKE '%search_string%' OR CONCAT(firstname,' ',name) LIKE '%search_string'%'
试试这个:

Select * from User where `name` LIKE '%search_string%' OR `firstname`
 LIKE '%search_string%' OR CONCAT(firstname,' ',name) LIKE '%search_string'%'

这肯定会对你有帮助

下面的方法首先计算所有可能的单词组合,然后将其与数据库匹配

     <?php


        $name ='Georges Clooney'; // you search string
        $words=explode(" ", $name);;

        function get_all_combination($arr, $temp_string, &$collect) {
            if ($temp_string != "") 
                $collect []= $temp_string;

            for ($i=0; $i<sizeof($arr);$i++) {
                $arrcopy = $arr;
                $elem = array_splice($arrcopy, $i, 1); // removes and returns the i'th element
                if (sizeof($arrcopy) > 0) {
                    get_all_combination($arrcopy, $temp_string ." " . $elem[0], $collect);
                } else {
                    $collect []= $temp_string. " " . $elem[0];
                }   
            }   
        }

        $collect = array();
        get_all_combination($words, "", $collect);


           /* 
            $collect now have all possible combination of search string

            Array
            (
                [0] =>  Georges
                [1] =>  Georges Clooney
                [2] =>  Clooney
                [3] =>  Clooney Georges
            )
            */

        $sql="SELECT * FROM user_info WHERE (firstname like '%".implode("%' OR firstname  like '%",$collect)."%' or name  like '%".implode("%' OR name like '%",$collect)."%')" ;






        ?>

这肯定会对您有所帮助

下面的方法首先计算所有可能的单词组合,然后将其与数据库匹配

     <?php


        $name ='Georges Clooney'; // you search string
        $words=explode(" ", $name);;

        function get_all_combination($arr, $temp_string, &$collect) {
            if ($temp_string != "") 
                $collect []= $temp_string;

            for ($i=0; $i<sizeof($arr);$i++) {
                $arrcopy = $arr;
                $elem = array_splice($arrcopy, $i, 1); // removes and returns the i'th element
                if (sizeof($arrcopy) > 0) {
                    get_all_combination($arrcopy, $temp_string ." " . $elem[0], $collect);
                } else {
                    $collect []= $temp_string. " " . $elem[0];
                }   
            }   
        }

        $collect = array();
        get_all_combination($words, "", $collect);


           /* 
            $collect now have all possible combination of search string

            Array
            (
                [0] =>  Georges
                [1] =>  Georges Clooney
                [2] =>  Clooney
                [3] =>  Clooney Georges
            )
            */

        $sql="SELECT * FROM user_info WHERE (firstname like '%".implode("%' OR firstname  like '%",$collect)."%' or name  like '%".implode("%' OR name like '%",$collect)."%')" ;






        ?>

这肯定会对您有所帮助

下面的方法首先计算所有可能的单词组合,然后将其与数据库匹配

     <?php


        $name ='Georges Clooney'; // you search string
        $words=explode(" ", $name);;

        function get_all_combination($arr, $temp_string, &$collect) {
            if ($temp_string != "") 
                $collect []= $temp_string;

            for ($i=0; $i<sizeof($arr);$i++) {
                $arrcopy = $arr;
                $elem = array_splice($arrcopy, $i, 1); // removes and returns the i'th element
                if (sizeof($arrcopy) > 0) {
                    get_all_combination($arrcopy, $temp_string ." " . $elem[0], $collect);
                } else {
                    $collect []= $temp_string. " " . $elem[0];
                }   
            }   
        }

        $collect = array();
        get_all_combination($words, "", $collect);


           /* 
            $collect now have all possible combination of search string

            Array
            (
                [0] =>  Georges
                [1] =>  Georges Clooney
                [2] =>  Clooney
                [3] =>  Clooney Georges
            )
            */

        $sql="SELECT * FROM user_info WHERE (firstname like '%".implode("%' OR firstname  like '%",$collect)."%' or name  like '%".implode("%' OR name like '%",$collect)."%')" ;






        ?>

这肯定会对您有所帮助

下面的方法首先计算所有可能的单词组合,然后将其与数据库匹配

     <?php


        $name ='Georges Clooney'; // you search string
        $words=explode(" ", $name);;

        function get_all_combination($arr, $temp_string, &$collect) {
            if ($temp_string != "") 
                $collect []= $temp_string;

            for ($i=0; $i<sizeof($arr);$i++) {
                $arrcopy = $arr;
                $elem = array_splice($arrcopy, $i, 1); // removes and returns the i'th element
                if (sizeof($arrcopy) > 0) {
                    get_all_combination($arrcopy, $temp_string ." " . $elem[0], $collect);
                } else {
                    $collect []= $temp_string. " " . $elem[0];
                }   
            }   
        }

        $collect = array();
        get_all_combination($words, "", $collect);


           /* 
            $collect now have all possible combination of search string

            Array
            (
                [0] =>  Georges
                [1] =>  Georges Clooney
                [2] =>  Clooney
                [3] =>  Clooney Georges
            )
            */

        $sql="SELECT * FROM user_info WHERE (firstname like '%".implode("%' OR firstname  like '%",$collect)."%' or name  like '%".implode("%' OR name like '%",$collect)."%')" ;






        ?>


LIKE
condition.你试过什么了吗
LIKE
condition.你试过什么了吗
LIKE
condition.你试过什么了吗
LIKE
condition.你试过什么了吗我有一个bug“被零除”,这是什么意思?我找不到你。既然我们没有除法,你怎么能得到一个零除法例外?我有一个错误“零除法”你是什么意思?我找不到你。既然我们没有除法,你怎么能得到一个零除法例外?我有一个错误“零除法”你是什么意思?我找不到你。既然我们没有除法,你怎么能得到一个零除法例外?我有一个错误“零除法”你是什么意思?我找不到你。由于我们没有除法,如何得到零除法异常?那么在将字符串传递给MySQL之前,您应该检查字符串@克莱门坦德劳德
georney
不像
georgeclooney
geor
会是,ney也会是。两个词在一起不匹配。。。分开的词会匹配。。。想一想,至少自己做一些工作是的,所以,我不想要像;)然后:如我所说,检查字符串,如果有空格,如果有,可以分解字符串,并将其与另一个参数一起传递给SQL查询。。。来吧,没那么难;-)@ClémentAndraud所以看起来你不想知道
我如何用SQL做到这一点?
,而是,
有人能用SQL帮我做到这一点吗?
那么你应该在将字符串传递给MySQL之前检查它@克莱门坦德劳德
georney
不像
georgeclooney
geor
会是,ney也会是。两个词在一起不匹配。。。分开的词会匹配。。。想一想,至少自己做一些工作是的,所以,我不想要像;)然后:如我所说,检查字符串,如果有空格,如果有,可以分解字符串,并将其与另一个参数一起传递给SQL查询。。。来吧,没那么难;-)@ClémentAndraud所以看起来你不想知道
我如何用SQL做到这一点?
,而是,
有人能用SQL帮我做到这一点吗?
那么你应该在将字符串传递给MySQL之前检查它@克莱门坦德劳德
georney
不像
georgeclooney
geor
会是,ney也会是。两个词在一起不匹配。。。分开的词会匹配。。。想一想,至少自己做一些工作是的,所以,我不想要像;)然后:如我所说,检查字符串,如果有空格,如果有,可以分解字符串,并将其与另一个参数一起传递给SQL查询。。。来吧,没那么难;-)@ClémentAndraud看来你不想要