Python MySQL - LIKE() operator
Last Updated :
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:
Pattern | Meaning |
---|
'a%' | Match strings which start with 'a' |
'%a' | Match strings with end with 'a' |
'a%t' | Match strings which contain the start with 'a' and end with 't'. |
'%wow%' | Match strings which contain the substring 'wow' in them at any position. |
'_wow%' | Match strings which contain the substring 'wow' in them at the second position. |
'_a%' | Match strings which contain 'a' at the second position. |
'a_ _%' | Match strings which start with 'a' and contain at least 2 more characters. |
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 module
import mysql.connector
# establish connection
database = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="gfg"
)
# creating cursor object
cur_object = database.cursor()
print("like operator address starts with G")
# query
find = "SELECT * from itdept where Address like 'G%' "
# execute the query
cur_object.execute(find)
# fetching all results
data = cur_object.fetchall()
for i in data:
print(i[0], i[1], i[2], i[3], sep="--")
# Close database connection
database.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 module
import mysql.connector
# establish connection
database = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="gfg"
)
# creating cursor object
cur_object = database.cursor()
print("like operator name starts with H and ends with A")
# query
find = "SELECT * from itdept where Name like 'H%A' "
# execute the query
cur_object.execute(find)
# fetching all results
data = cur_object.fetchall()
for i in data:
print(i[0], i[1], i[2], i[3], sep="--")
# close database connection
database.close()
Output:
Example 3:
In this program, we display all the rows having three-lettered addressees in the table.
Python3
# import mysql.connector module
import mysql.connector
# establish connection
database = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="gfg"
)
# creating cursor object
cur_object = database.cursor()
print("like operator address has three letters only")
# query
find = "SELECT * from itdept where Address like '___' "
# execute the query
cur_object.execute(find)
# fetching all results
data = cur_object.fetchall()
for i in data:
print(i[0], i[1], i[2], i[3], sep="--")
# close database connection
database.close()
Output:
Similar Reads
Python Logical Operators Python logical operators are used to combine conditional statements, allowing you to perform operations based on multiple conditions. These Python operators, alongside arithmetic operators, are special symbols used to carry out computations on values and variables. In this article, we will discuss l
6 min read
Python MySQL - BETWEEN and IN Operator In this article, we are going to see the database operations BETWEEN and IN operators in MySQL using Python. Both of these operators are used with the WHERE query to check if an expression is within a range or in a list of values in SQL. We are going to use the below table to perform various operati
3 min read
Python 3 - Logical Operators Logical Operators are used to perform certain logical operations on values and variables. These are the special reserved keywords that carry out some logical computations. The value the operator operates on is known as Operand. In Python, they are used on conditional statements (either True or False
4 min read
Python Operators In Python programming, Operators in general are used to perform operations on values and variables. These are standard symbols used for logical and arithmetic operations. In this article, we will look into different types of Python operators. OPERATORS: These are the special symbols. Eg- + , * , /,
6 min read
Ternary Operator in Python The ternary operator in Python allows us to perform conditional checks and assign values or perform operations on a single line. It is also known as a conditional expression because it evaluates a condition and returns one value if the condition is True and another if it is False.Basic Example of Te
5 min read