0

I have a form where I want a user to enter one or more words. These words should then match mutiple columns in a MySQL database.

I have started to build some code but I'm stuck.

<?php
  $term = $_SESSION['session_searchstring']; //Let's say that session is John Doe
  $searchterm = explode(' ',$term);

  $searchFieldName = "name";
  $searchCondition = "$searchFieldName LIKE '%" . implode("%' OR $searchFieldName LIKE '%", $searchterm) . "%'";

  $sql = "SELECT * FROM students WHERE $searchCondition;";

  echo $sql; //Echo to test what mysql_query would look like

?>

The above code will output:

SELECT * FROM students WHERE name LIKE '%John%' OR name LIKE '%Doe%'; 

The problem is that I want to search in multiple columns ($searchFieldName). I have for example

customer_firstname
customer_lastname

And I want to match my searchstring against the content of both columns.. How would I continue?

4

3 回答 3

1

如果您的表是 MyIsam 类型,或者您可以将其转换为 MyIsam,请使用MySQL 全文搜索。如果没有,无论如何,您可以构建一个长查询,例如

SELECT * FROM students WHERE name LIKE '%John%' OR name LIKE '%Doe%' OR lastname LIKE "%John%" OR lastname LIKE "%Doe%"

或将您的列合并到另一个只是为了搜索(但这两者都不是首选)。一个好的方法是使用像Sphinx这样的全文搜索引擎。

于 2012-07-18T14:07:58.203 回答
1

也许

  $term = $_SESSION['session_searchstring']; //Let's say that session is John Doe
  $searchterm = explode(' ',$term);

  $searchColumns = array("customer_firstname","customer_lastname");

  for($i = 0; $i < count($searchColumns); $i++)
    {
        $searchFieldName = $searchColumns[$i];
        $searchCondition .= "($searchFieldName LIKE '%" . implode("%' OR $searchFieldName LIKE '%", $searchterm) . "%')";
        if($i+1 < count($searchColumns)) $searchCondition .= " OR ";
     }

  $sql = "SELECT * FROM students WHERE $searchCondition;";

  echo $sql; //Echo to test what mysql_query would look like

生产

SELECT * FROM students WHERE (customer_firstname LIKE '%John%' OR customer_firstname LIKE '%Doe%') OR (customer_lastname LIKE '%John%' OR customer_lastname LIKE '%Doe%');

于 2012-07-18T14:09:34.053 回答
0

在我的情况下,我需要所有搜索短语/术语来匹配至少一列,没有搜索短语/术语可能是不匹配的。

我最终通过以下方式调整了Kermit的示例:

public function getRawWhereFilterForColumns($filter, $search_columns)
{
  $search_terms = explode(' ', $filter);
  $search_condition = "";

  for ($i = 0; $i < count($search_terms); $i++) {
    $term = $search_terms[$i];

    for ($j = 0; $j < count($search_columns); $j++) {
      if ($j == 0) $search_condition .= "(";
      $search_field_name = $search_columns[$j];
      $search_condition .= "$search_field_name LIKE '%" . $term . "%'";
      if ($j + 1 < count($search_columns)) $search_condition .= " OR ";
      if ($j + 1 == count($search_columns)) $search_condition .= ")";
    }
    if ($i + 1 < count($search_terms)) $search_condition .= " AND ";
  }
  return $search_condition;
}

我只需要 Where 子句的内容,因为我使用的是 Laravel,并且可以将其放入 rawWhere 方法中。

用法:

$search_condition = $this->getRawWhereFilterForColumns
    ("John Doe", array("column1", "column2"));

产生

    (column1 LIKE '%John%' OR column2 LIKE '%John%') 
AND (column1 LIKE '%Doe%' OR column2 LIKE '%Doe%')

最后,您可以以任何适合您的方式使用此 $search_condition,例如:

 $sql = "SELECT * FROM students WHERE $search_condition;";

或者在我的 Laravel 案例中:

 $modelInstances = Model::whereRaw
     ($search_condition)->paginate(self::ITEMS_PER_PAGE);

对于David或任何其他访问此线程的人(例如我)来说,这可能是一个改进的解决方案,即使它是在原始帖子发布将近两年之后。

于 2014-04-15T07:45:41.120 回答