简体   繁体   English

如何在mysql准备的语句中正确使用WHERE LIKE?

[英]How do I use WHERE LIKE properly in a mysql prepared statement?

I'm trying to puzzle out how to add a LIKE to this prepared statement. 我想弄清楚如何在此准备好的语句中添加LIKE

SELECT convention_name FROM events_master 
WHERE (convention_name = ?);

The goal is to see if the convention name is similar to something that already exists in convention_name . 我们的目标是看是否约定名称类似于已经存在的东西convention_name I do understand that it checks for an exact match already. 我确实知道它已经检查了完全匹配。

Thanks in advance. 提前致谢。

For example: 例如:

 String query = "SELECT convention_name FROM events_master WHERE convention_name like ?";
 PreparedStatement stm = conn.prepareStatement(query);
 stm.setString(1, "%car%");

Your argument may contain wildchar %, then it will find for example card, scart .. 您的参数可能包含wildchar%,然后它将找到例如card,scart ..

If this substring match is not enough, then you can use soundex algorithm to find similar strings. 如果此子字符串匹配不够,则可以使用soundex算法查找相似的字符串。 See how to compute similarity between two strings in MYSQL for more options. 有关更多选项,请参见如何在MYSQL中计算两个字符串之间的相似度

SELECT convention_name FROM events_master WHERE (convention_name LIKE 'partofname' + '%'); SELECT Convention_name FROM events_master WHERE(convention_name LIKE'partofname'+'%');

The % character is a wild char so if you put it in the back it will search for anything that begins with 'partofname' + blah appended to it. %字符是通配符,因此如果将其放在后面,它将搜索以'partofname'+ blah开头的任何内容。

if it's Like = '%partofname%' then partofname could have characters before or after it. 如果它的Like ='%partofname%',则partofname之前或之后可以包含字符。

If SQL LIKE clause is used along with % characters, then it will work like a meta character (*) in UNIX while listing out all the files or directories at command prompt. 如果将SQL LIKE子句与%字符一起使用,则它将在UNIX中像元字符(*)一样工作,同时在命令提示符下列出所有文件或目录。

Without a % character, LIKE clause is very similar to equals sign along with WHERE clause. 没有%字符,LIKE子句与WHERE子句非常等同于等号。

$sql = $dbh->prepare(SELECT convention_name FROM events_master 
WHERE convention_name LIKE ?");

$sql->execute(array('%'.$yourSearchString.'%'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM