如果搜索输入中有空格,我很难获得显示结果的查询。数据库中有一列包含两个字符串。问题是,如果字符串中有空格,我不知道如何使用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%')";
不管我在搜索输入中输入了什么,最终还是查询了所有内容。
我不确定我需要在这里做什么。