MySQL 使用 LIKE 和空格查询
MySQL query using LIKE and spaces
如果搜索输入中有 space,我无法获取显示结果的查询。数据库有一个列,其中有两个字符串。问题是如果字符串中有 spaces,我不知道如何使用 LIKE 来过滤信息。我不确定是否需要 MATCH AGAINST 因为我对 table 中的两个单独的数据列不感兴趣,我对一个由 [=27= 分隔的两个词的列感兴趣], 该列是 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
索引。
旁注:我不知道为什么您会在 COUNT(*)
不能 return 多行且没有任何其他字段的情况下在查询中应用排序或限制select.
如果搜索输入中有 space,我无法获取显示结果的查询。数据库有一个列,其中有两个字符串。问题是如果字符串中有 spaces,我不知道如何使用 LIKE 来过滤信息。我不确定是否需要 MATCH AGAINST 因为我对 table 中的两个单独的数据列不感兴趣,我对一个由 [=27= 分隔的两个词的列感兴趣], 该列是 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
索引。
旁注:我不知道为什么您会在 COUNT(*)
不能 return 多行且没有任何其他字段的情况下在查询中应用排序或限制select.