Skip to content Skip to sidebar Skip to footer

Mysql: Conduct A Basic Search

I have a names table in my database and I would wish to conduct a fuzzy search on it for example my database contains: Name ID John Smith 1 Edward Smith 2 Gabriel Gray 3

Solution 1:

In simplest form, you'd use the LIKE comparison:

SELECT*FROMtableWHERE name LIKE'%smith%';

More elaborate searches can de done with FULLTEXT index (large amounts of text), SOUNDEX() (works on words in the english language, matching on other languages is everything from 'somewhat workable' to 'terrible'), levenshtein distance of words etc.

Solution 2:

import MySQLdb
search_str = 'smith'
conn = MySQLdb.connect(host="localhost", user="me",passwd="pw",db="mydb")
c = conn.cursor()
c.execute("SELECT name FROM mytable WHERE name LIKE %s", '%' + search_str + '%')
c.fetchall()
c.close()
conn.close()

Post a Comment for "Mysql: Conduct A Basic Search"