[英]MySQL query using LIKE and spaces
如果搜索輸入中有空格,我將無法查詢顯示結果。 數據庫中有一個包含兩個字符串的列。 問題是,如果字符串中有空格,我不知道如何使用LIKE來過濾信息。 我不確定是否需要MATCH AGAINST,因為我對表中的數據的兩個獨立列不感興趣,我對由兩個單詞隔開一個空格的列感興趣,該列為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和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."))";
編輯:也嘗試了此-
$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%')";
盡管我輸入了搜索輸入,但最終還是查詢了所有內容。
我不確定在這里需要做什么。
您實際上應該采用自然語言搜索方法來解決此問題。 使用這種方法的查詢如下所示:
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文檔 。
旁注:我不知道為什么您要在COUNT(*)
的上下文中在查詢中應用排序或限制,而在選擇中沒有任何其他字段的情況下不能返回多行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.