简体   繁体   English

使用LIKE和空格的MySQL查询

[英]MySQL query using LIKE and spaces

I'm having trouble getting a query to display results if there are spaces in the search input. 如果搜索输入中有空格,我将无法查询显示结果。 The database has a column that has two strings in it. 数据库中有一个包含两个字符串的列。 The issue is that I don't know how to use LIKE to filter information if it has spaces in the string. 问题是,如果字符串中有空格,我不知道如何使用LIKE来过滤信息。 I'm not sure if MATCH AGAINST is needed for this since I'm not interested in two separate columns of data in the table, I'm interested in one column that has two words separated by a space, the column is fullNameNormal. 我不确定是否需要MATCH AGAINST,因为我对表中的数据的两个独立列不感兴趣,我对由两个单词隔开一个空格的列感兴趣,该列为fullNameNormal。

EDIT: I just realized that I forgot that the search feature is actually the input that is for $search. 编辑:我刚刚意识到,我忘记了搜索功能实际上是$ search的输入。 Originally documented it as $letter, but that is for a letter search only. 最初将其记录为$ letter,但这仅用于字母搜索。

$search = '';
        if(isset($_REQUEST['search'])){
            $search = substr($this->encode($_REQUEST['search']), 0, 50);
        }
$letter = '';
if (isset($_REQUEST['letter'])) {
    $letter = substr($_REQUEST['letter'], 0, 1);
}
// Get total amount of records for current search...for paging
$total = 0;
$sql = "SELECT COUNT(*) FROM " . $wpdb->prefix . "example_directory";
$clean_where = " WHERE dirListing = 'public' AND active = 1 AND (((employeeType = 'faculty') AND (shortPositionCode NOT IN ('47', '49', '58', '59')) AND (status IN ('fullTime', 'partTime', 'proRata'))) OR ((employeeType = 'staff') AND (shortPositionCode NOT IN ('47', '49', '58', '59')) AND (status IN ('fullTime', 'partTime'))))";
$order = " ORDER BY lastName, firstName, department";
$limit_query = $wpdb->prepare(" LIMIT %d, %d", $start, $limit);
$where = "";
$args = array();
if ($search != '') {
    $where = " AND (lastName LIKE %s OR firstName LIKE %s OR department LIKE %s OR fullName LIKE %s OR nickName LIKE %s OR jobTitle LIKE %s OR phoneExt LIKE %s OR phone LIKE %s OR email LIKE %s OR locationBuilding LIKE %s OR locationBuildingAbbrev LIKE %s OR locationRoom LIKE %s or fullNameNormal LIKE %s)";
    $arg = '%' . $search . '%';
    $args = array($arg, $arg, $arg, $arg, $arg, $arg, $arg, $arg, $arg, $arg, $arg, $arg, $arg);
} elseif ($letter != '') {
    $where = " AND lastName LIKE %s";
    $arg = $letter . '%';
    $args = array($arg);
} else {
    $where = "";
}
    $where = $wpdb->prepare($where, $args);
    $total = $wpdb->get_var($sql . $clean_where . $where . $order);

As previously stated, I'm only interested in fullNameNormal which would be as an example "John Smith" in the cell, but it seems that there's no way to use LIKE and limit it to only what I put in the input that becomes $search. 如前所述,我只对fullNameNormal感兴趣,例如在单元格中以“ John Smith”为例,但似乎没有办法使用LIKE并将其限制为仅输入到我变成$ search的内容中。 I also tried adding MATCH and AGAINST like this - 我还尝试过像这样添加MATCH和AGAINST-

$where = " AND (lastName LIKE %s OR firstName LIKE %s OR department LIKE %s OR fullName LIKE %s OR nickName LIKE %s OR jobTitle LIKE %s OR phoneExt LIKE %s OR phone LIKE %s OR email LIKE %s OR locationBuilding LIKE %s OR locationBuildingAbbrev LIKE %s OR locationRoom LIKE %s OR MATCH(fullNameNormal) AGAINST (".$search."))";

EDIT: Also tried this - 编辑:也尝试了此-

$where = " AND (lastName LIKE %s OR firstName LIKE %s OR department LIKE %s OR fullName LIKE %s OR nickName LIKE %s OR jobTitle LIKE %s OR phoneExt LIKE %s OR phone LIKE %s OR email LIKE %s OR locationBuilding LIKE %s OR locationBuildingAbbrev LIKE %s OR locationRoom LIKE %s OR (fullNameNormal LIKE 'something%' AND fullNameNormal LIKE '% something%')";

Ended up query everything despite what I put in the search input. 尽管我输入了搜索输入,但最终还是查询了所有内容。

I'm unsure of what I need to do here. 我不确定在这里需要做什么。

You should really be taking a natural language search approach to this problem. 您实际上应该采用自然语言搜索方法来解决此问题。 A query using this approach would look like: 使用这种方法的查询如下所示:

SELECT COUNT(*) FROM *example_directory
WHERE MATCH (
  lastName,
  firstName,
  department,
  fullName,
  nickName,
  jobTitle,
  phoneExt,
  phone,
  email,
  locationBuilding,
  locationBuildingAbbrev,
  locationRoom,
  fullNameNormal
) AGAINST (? IN NATURAL LANGUAGE MODE)
AND  ... /* other filter conditions */

Where ? ? is your search phrase. 是您的搜索词组。

You will need to create a FULLTEXT index on the columns in your search. 您将需要在搜索的列上创建FULLTEXT索引。

See MySQL Documentation . 参见MySQL文档

Side note: I have no idea why you would be applying ordering or limits in your query within the context of COUNT(*) not being able to return more than one row without any additional fields in select. 旁注:我不知道为什么您要在COUNT(*)的上下文中在查询中应用排序或限制,而在选择中没有任何其他字段的情况下不能返回多行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM