Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.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
Mysql 在两列中搜索单词_Mysql_Pdo - Fatal编程技术网

Mysql 在两列中搜索单词

Mysql 在两列中搜索单词,mysql,pdo,Mysql,Pdo,我正在编写一个搜索脚本,这是 我的php代码: $search_string = preg_replace("/[^A-Za-z0-9çğıöüş]/", " ", $_POST['query']); $search_param = '%'.$search_string.'%'; // Do Search $result_array = search_by_name($search_param); //functions 我有这样一个问题: SELECT user_id AS id,

我正在编写一个搜索脚本,这是

我的php代码:

$search_string = preg_replace("/[^A-Za-z0-9çğıöüş]/", " ", $_POST['query']);

$search_param = '%'.$search_string.'%';
// Do Search
$result_array = search_by_name($search_param); //functions
我有这样一个问题:

SELECT user_id AS id,  
       user_firstname AS firstname,  
       user_lastname AS lastname,  
       user_profile_picture AS picture  
FROM users WHERE  
       user_firstname LIKE :user_firstname  
    OR user_lastname LIKE :user_lastname  

 $result->bindParam(':user_firstname', $search_param, PDO::PARAM_STR);
 $result->bindParam(':user_lastname', $search_param, PDO::PARAM_STR);
当您编写单词时,此查询会在user_firstname列或user_lastname列中找到它。 比如说,

 `user_firstname    user_lastname
  --------------    -------------
  james             wood
  jimmy             james
  george            wood  
在这种情况下,如果我在搜索区域中写“james”,我会找到“james wood”和“jimmy james”。但是,如果我写“james wood”,我将找不到任何东西,因为我正在user_firstname列或user_lastname colum中搜索它,其中不包含“james wood”

我想做的是能够搜索全名并得到结果。我该怎么做


`

编辑:阅读重写的问题,这将成为一个php问题。假设要搜索的名称总是输入“first-last”(而不是“last-first”),您应该检查输入,如果输入有两个或两个以上的单词,请将第一个空格处的输入拆分为两个变量:
user\u firstname
user\u lastname
。如果输入只有一个单词,请将其放入变量
user\eithername
。 那么您的SQL将是:

SELECT user_id AS id,  
       user_firstname AS firstname,  
       user_lastname AS lastname,  
       user_profile_picture AS picture  
FROM users WHERE  
       ( user_firstname LIKE :user_firstname  
    AND user_lastname  LIKE :user_lastname )
    OR user_firstname LIKE :user_eithername  
    OR user_lastname  LIKE :user_eithername  

这基本上是可行的,但仍然会有一些问题-搜索“梵高”的姓氏(仅)将被解释为
firstname=van
lastname=Gogh
,这是不正确的。用户只需输入“Gogh”,但像“Gogh%”这样的
仍然与“van Gogh”不匹配。在不了解更多需求、数据、用户基础等的情况下,很难提供完整的解决方案。

创建一个新列,例如user\u first\u last\u name。把名字和姓氏放在一起。然后从该列中搜索。

实际上,只需搜索一个字符串。这是“詹姆斯·伍德”。如果我写“james wood”,它将在用户名或用户名中搜索。我需要在user_firstname+user_lastname中搜索它