Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.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 mysql将多列中的(多个)单词与空格匹配_Php_Mysql - Fatal编程技术网

Php mysql将多列中的(多个)单词与空格匹配

Php mysql将多列中的(多个)单词与空格匹配,php,mysql,Php,Mysql,我正在试图找出我的查询需要什么样的函数和方法,但仍然不确定是否使用了Like、concat或what part等 我的情况就是这样 1.)我有多个栏(国家、城市、州、地点) 2.)仅1个搜索输入 3.)搜索输入可以是一个单词,或者多个单词也可以忽略空格(例如,“中心城市或中心城市或中心城市费城”)等 它将返回与不同列中的单词匹配的行 以下是我的尝试,但目前没有任何回报。谢谢您的时间 Php: 更新了搜索地址字段中每个单词的答案: $searchaddress = "some address to

我正在试图找出我的查询需要什么样的函数和方法,但仍然不确定是否使用了Like、concat或what part等

我的情况就是这样

1.)我有多个栏(国家、城市、州、地点) 2.)仅1个搜索输入 3.)搜索输入可以是一个单词,或者多个单词也可以忽略空格(例如,“中心城市或中心城市或中心城市费城”)等

它将返回与不同列中的单词匹配的行

以下是我的尝试,但目前没有任何回报。谢谢您的时间

Php:


更新了搜索地址字段中每个单词的答案:

$searchaddress = "some address to find";
$address_parts = explode(" ", trim($searchaddress));
$sql_parts = array();
foreach($address_parts as $part) {
    $sql_parts[] = 'full_address LIKE "%'.$part.'%"';
}
$query = 'SELECT *, CONCAT(country,state,city,Location) AS full_address FROM Listing WHERE `Status` = "Open" HAVING '.implode(' OR ', $sql_parts);

如果不知道您的确切数据以及
$searchaddress
是什么样子,就很难说出它失败的原因

您正在谈论输入空格,但只需传入一个searchtag,像“%something other%”这样的表达式
将不会忽略空格

如果您希望在所有给定单词匹配的情况下获得最少的结果,您应该投入更多的精力,并使用
或/和
搜索标记/列的组合。您可以通过编程实现这一点

假设您输入了2个关键字:
Center Detroid
,您基本上希望生成searchquery:

FROM Listing WHERE 
 ( 
   country LIKE '%Center%' OR
   state LIKE '%Center%' OR 
   city LIKE '%Center%' OR 
   Location LIKE '%Center%'
 ) 
 AND
 ( 
   country LIKE '%Detroid%' OR
   state LIKE '%Detroid%' OR 
   city LIKE '%Detroid%' OR 
   Location LIKE '%Detroid%'
 )
要实现这一目标,您需要知道两件事:

  • 要搜索的字段名
  • 关键词
然后,以下代码段将根据需要生成where零件:

$search = "Detroid City Center";
$keywords = explode (" ", $search);
$columns = array("country", "state", "city", "location");

$andParts = array();
foreach ($keywords AS $keyword){
  $orParts = array();
    foreach($columns AS $column){
      $orParts[] = $column . " LIKE '%" . mysql_real_escape_string($keyword) . "%'";
    }
    $andParts[]= "(" . implode($orParts, " OR ") . ")";
}
$and = implode ($andParts, " AND ");

echo $and;
数组中给出的示例将产生

(
  country LIKE '%Center%' OR 
  state LIKE '%Center%' OR 
  city LIKE '%Center%' OR 
  location LIKE '%Center%'
) 
AND 
(
   country LIKE '%City%' OR 
   state LIKE '%City%' OR 
   city LIKE '%City%' OR
   location LIKE '%City%'
 ) 
 AND 
 (
   country LIKE '%Detroid%' OR 
   state LIKE '%Detroid%' OR 
   city LIKE '%Detroid%' OR 
   location LIKE '%Detroid%'
 )

这将匹配中心、城市或Detroid在所有行中至少出现一次的任何行(搜索-)每行字段。

Status不是保留的,因为Status是一个在MySQL中不是真正的关键字但有意义的词。例如“SHOW Status”,我尝试了它,它完全相同。我认为我的搜索条件不正确,但我不确定如何更改它。因为目前它将我的搜索字段视为一个单词,但是我需要它来搜索所有不同的单词。我尝试完全删除Status=“Open”,它仍然不起作用。你的意思是通过所有地址列搜索输入中的每个单词?是的,检查每个列中的每个单词,因此最后只返回与所有单词匹配的行,如(费城市中心)将只返回与该位置和城市匹配的结果。
(
  country LIKE '%Center%' OR 
  state LIKE '%Center%' OR 
  city LIKE '%Center%' OR 
  location LIKE '%Center%'
) 
AND 
(
   country LIKE '%City%' OR 
   state LIKE '%City%' OR 
   city LIKE '%City%' OR
   location LIKE '%City%'
 ) 
 AND 
 (
   country LIKE '%Detroid%' OR 
   state LIKE '%Detroid%' OR 
   city LIKE '%Detroid%' OR 
   location LIKE '%Detroid%'
 )