我必须在2个不同的列中查找关键字,所以我决定尝试使用mysql MATCH
命令。但是,如果我尝试搜索" bcd":
$sql = "SELECT id,type,size,description FROM products
WHERE MATCH (title,description) AGAINST ('+*bcd*' IN BOOLEAN MODE)";
MATCH
会找到bcdef
,但不是abcde
。它在某种程度上不接受关键字开头的通配符。
有没有办法将通配符应用到匹配关键字的开头?或者以不同的方式一次搜索两列会更好吗?
答案 0 :(得分:0)
试试:
SELECT id,type,size,description FROM products
WHERE title LIKE '%bcd%'
Or description LIKE '%bcd%'
demo有两个关键字
答案 1 :(得分:0)
使用 mysqli prepared statements
,因为在最近的PHP版本中不推荐使用mysql_ *。如果您的查询,只需使用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