Python MySQL - LIKE() operator - GeeksforGeeks

28 Apr, 2021
In this article, we will discuss the use of LIKE operator in MySQL using Python language.
Sometimes we may require tuples from the database which match certain patterns. For example, we may wish to retrieve all columns where the tuples start with the letter ‘y’, or start with ‘b’ and end with ‘l’, or even more complicated and restrictive string patterns. This is where the LIKE Clause comes to the rescue, often coupled with the WHERE Clause in SQL.
There are two kinds of wildcards used to filter out the results:
The percent sign (%): Used to match zero or more characters. (Variable Length)
The underscore sign (_): Used to match exactly one character. (Fixed Length)
Syntax:
SELECT column1, column2, ...,columnn
FROM table_name
WHERE columnn LIKE pattern;
The following are the rules for pattern matching with the LIKE Clause: 
In order to use LIKE operations we are going to use the below table:
Below are various examples that depict how to use LIKE operator in Python MySQL.
Example 1:
Program to display rows where the address starts with the letter G in the itdept table.
Python3
# import mysql.connector moduleimport mysql.connector  # establish connectiondatabase = mysql.connector.connect(    host="localhost",    user="root",    password="",    database="gfg")  # creating cursor objectcur_object = database.cursor()print("like operator address starts with G")  #  queryfind = "SELECT * from itdept where Address like 'G%' "  # execute the querycur_object.execute(find)  # fetching all resultsdata = cur_object.fetchall()for i in data:    print(i[0], i[1], i[2], i[3], sep="--")  # Close database connectiondatabase.close()
Output:
Example 2:
Here we display all the rows where the name begins with the letter H and ends with the letter A in the table.
Python3
# import mysql.connector moduleimport mysql.connector  # establish connectiondatabase = mysql.connector.connect(    host="localhost",    user="root",    password="",    database="gfg")  # creating cursor objectcur_object = database.cursor()print("like operator name starts with H and ends with A")  #  queryfind = "SELECT * from itdept where Name like 'H%A' "  # execute the querycur_object.execute(find)  # fetching all resultsdata = cur_object.fetchall()for i in data:    print(i[0], i[1], i[2], i[3], sep="--")  # close database connectiondatabase.close()
Output:
Example 3:
In this program, we display all the rows having three-lettered addressees in the table.
Python3
# import mysql.connector moduleimport mysql.connector  # establish connectiondatabase = mysql.connector.connect(    host="localhost",    user="root",    password="",    database="gfg")  # creating cursor objectcur_object = database.cursor()print("like operator address has three letters only")  #  queryfind = "SELECT * from itdept where Address like '___' "  # execute the querycur_object.execute(find)  # fetching all resultsdata = cur_object.fetchall()for i in data:    print(i[0], i[1], i[2], i[3], sep="--")  # close database connectiondatabase.close()
Output:
Python-mySQL
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()