[英]Matching multiple words in mysql query using “LIKE”
My application allows searching of articles. 我的应用程序允许搜索文章。 So when a user enters the string "articlex" the query works & results are show but when multiple words are entered "articlex articley" the query shows no results.
因此,当用户输入字符串“ articlex”时,查询有效并显示结果,但是当输入多个单词“ articlex articley”时,查询将不显示结果。
Cuurently I'm using this query 我正在使用此查询
$querySub = 'SELECT * FROM table WHERE (col1 LIKE "%'.$search_string.'%") OR (col2 LIKE "%'.$search_string.'%") OR (col3 LIKE "%'.$search_string.'%")';
Where $search_string
only contains the input by user 其中
$search_string
仅包含用户输入
How can I make the query applicable for multiple words as well? 如何使查询也适用于多个单词?
Example for PDO
PDO
示例
/* Assume $pdo is already prepared as PDO instance. */
// search query split by spaces
$user_input = 'foo bar baz';
// create string for PDO::prepare()
$sql = 'SELECT * FROM testTable WHERE ';
$wheres = $values = array();
foreach (array_filter(explode(' ', $user_input), 'strlen') as $keyword) {
$wheres[] = 'col1 LIKE ?';
$values[] = '%' . addcslashes($keyword, '%_\\') . '%'; // this is escape for LIKE search
}
$sql .= $wheres ? implode(' OR ', $wheres) : '1';
// bind values and execute
$stmt = $pdo->prepare($sql);
$stmt->execute($values);
Example for mysql_**
functions (deprecated) mysql_**
函数示例(不建议使用)
/* Assume $link is already prepared as MySQL resource. */
// search query split by spaces
$user_input = 'foo bar baz';
// create string for mysql_auery()
$sql = 'SELECT * FROM testTable WHERE ';
foreach (array_filter(explode(' ', $user_input), 'strlen') as $keyword) {
$wheres[] = 'col1 LIKE ' . mysql_real_escape_string(
'%' . addcslashes($keyword, '%_\\') . '%',
$link
);
}
$sql .= !empty($wheres) ? implode(' OR ', $wheres) : '1';
// execute
$result = mysql_query($sql, $link);
While using variables for like query, you could use 在使用变量进行类似查询时,您可以使用
Select * from table where keyword LIKE '%{$to_be_searched}%'
从表中选择*,其中关键字LIKE'%{$ to_be_searched}%'
use only one "%" at the end. 最后只使用一个“%”。 like
喜欢
$querySub = SELECT * FROM table WHERE (col1 LIKE '$search_string%'");
This will search for both "articlex" and "articley" 这将同时搜索“ articlex”和“ articley”
delimiter // CREATE PROCEDURE proc_advancesearch
(IN p_data
VARCHAR(80) , IN act_mode
VARCHAR(10)) 定界符// CREATE PROCEDURE
proc_advancesearch
(IN p_data
VARCHAR(80),IN act_mode
VARCHAR(10))
begin 开始
declare p_s_data varchar(250); 声明p_s_data varchar(250); SET sql_mode='NO_ENGINE_SUBSTITUTION';
SET sql_mode ='NO_ENGINE_SUBSTITUTION';
set p_s_data= REPLACE(p_data,' ','% '); 设置p_s_data = REPLACE(p_data,'','%'); set p_s_data=concat('% ',p_s_data,'%');
设置p_s_data = concat('%',p_s_data,'%');
select prodtmappid,ProId, ProductName,FinalPrice, fn_image(prodtmappid) as image from tblproductmapforlisting where ProStatus='A' and ( concat(ProductName,' ',brandname,' ',CatName) like (p_s_data) 从tblproductmap中选择prodtmappid,ProId,ProductName,FinalPrice,fn_image(prodtmappid)作为映像,以列出ProStatus ='A'和(concat(ProductName,'',brandname,'',CatName)像(p_s_data)
) )
or ProductName like concat(p_data,'%')) group by tblproductmapforlisting.ProId order by propriority asc ; 或诸如concat(p_data,'%'))之类的ProductName按tblproductmapforlisting分组。ProId按专有性asc排序;
end 结束
您可以为此使用REGEXP .....或者,如果您想进行多个单词搜索,则需要按空格展开搜索字符串,然后使用LIKE从表中搜索此字符串。
if you search "articlex articley" then it will search whole word in LIKE operator
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.