代码之家  ›  专栏  ›  技术社区  ›  Alex Howell

使用LIKE和空格的MySQL查询

  •  1
  • Alex Howell  · 技术社区  · 10 年前

    如果搜索输入中有空格,我很难获得显示结果的查询。数据库中有一列包含两个字符串。问题是,如果字符串中有空格,我不知道如何使用LIKE来过滤信息。我不确定这是否需要MATCH ANTION,因为我对表中两个单独的数据列不感兴趣,我对一列有两个用空格分隔的单词感兴趣,该列是fullNameNormal。

    编辑:我刚刚意识到我忘记了搜索功能实际上是$search的输入。最初记录为$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);
    

    如前所述,我只对fullNameNormal感兴趣,它将作为单元格中的示例“John Smith”,但似乎没有办法使用LIKE并将其限制为我输入的内容,即$search。我也尝试过像这样添加MATCH和ACTION-

    $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."))";
    

    编辑:也尝试过这个-

    $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%')";
    

    不管我在搜索输入中输入了什么,最终还是查询了所有内容。

    我不确定我需要在这里做什么。

    1 回复  |  直到 10 年前
        1
  •  1
  •   Mike Brant    10 年前

    你真的应该用自然语言搜索的方法来解决这个问题。 使用此方法的查询如下所示:

    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 */
    

    哪里 ? 是您的搜索短语。

    您需要创建 FULLTEXT 对搜索中的列进行索引。

    看见 MySQL Documentation .

    附带说明:我不知道您为什么要在查询上下文中应用排序或限制 COUNT(*) 如果select中没有任何其他字段,则无法返回多行。

    推荐文章