关键字开头的MySQL MATCH通配符


MySQL MATCH wildcard at the beginning of a keyword

我必须在两个不同的列中查找关键字,所以我决定尝试mysql MATCH命令。但是如果我尝试搜索"bcd":

$sql = "SELECT id,type,size,description FROM products 
WHERE MATCH (title,description) AGAINST ('+*bcd*' IN BOOLEAN MODE)";  

MATCH会找到bcdef,但不会找到abcde。它不接受关键字开头的通配符。

是否有方法将通配符应用于匹配关键字的开头还是用不同的方式同时搜索两列更好?

试试看:

   SELECT id,type,size,description FROM products 
           WHERE title       LIKE '%bcd%' 
           Or    description LIKE '%bcd%' 

两个关键字的演示

使用mysqli prepared statements,因为mysql_*在最近的PHP版本中已被弃用。同样,在查询的情况下,只需使用LIKE %%OR在多列中搜索字符串

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s'n", mysqli_connect_error());
    exit();
}
$string = "bcd";    
$stmt = $mysqli->prepare("SELECT id,type,size,description FROM products 
                         WHERE (title LIKE '%?%' OR description LIKE '%?%')");
$stmt->bind_param( "ss", $string, $string); 
// "ss" is a format string, each "s" means string
$stmt->execute();
$stmt->bind_result($col1, $col2);
// then fetch and close the statement