0

I'm trying to puzzle out how to add a LIKE to this prepared statement.

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. I do understand that it checks for an exact match already.

Thanks in advance.

Maelish
  • 1,600
  • 4
  • 18
  • 25

3 Answers3

0

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 ..

If this substring match is not enough, then you can use soundex algorithm to find similar strings. See how to compute similarity between two strings in MYSQL for more options.

Community
  • 1
  • 1
Leos Literak
  • 8,805
  • 19
  • 81
  • 156
0

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.

if it's Like = '%partofname%' then partofname could have characters before or after it.

sjramsay
  • 555
  • 1
  • 5
  • 12
0

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.

Without a % character, LIKE clause is very similar to equals sign along with WHERE clause.

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

$sql->execute(array('%'.$yourSearchString.'%'));
underscore
  • 6,495
  • 6
  • 39
  • 78