
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Explain the use of sql LIKE operator using MySQL in Python?
LIKE is a operator in MySQL. The LIKE operator is used with the WHERE statement to search for a specific pattern in the table.
Suppose you want to search for values which start with “a” in the table, the LIKE statement is used in such scenarios.
There are two wildcard characters used with the LIKE clause.
% − This sign represents zero,one or multiple characters.
_ (underscore) − This represents one single character.
Example
LIKE ‘a%’ - Searches for all the values starting with a.
LIKE ‘%a’ -Searches for all the values which end with a.
LIKE ‘_a%’ - Searches for all the values where ‘a’ is at the second position. The _ represents a single character, the second character must be ‘a’ and after a there might be some characters present or not.
Syntax
SELECT * FROM table_name WHERE column_name LIKE pattern
Steps invloved in searching data based on some pattern in a table using MySQL in python
import MySQL connector
establish connection with the connector using connect()
create the cursor object using cursor() method
create a query using the appropriate mysql statements
execute the SQL query using execute() method
close the connection
Suppose we the following table named “Students” −
Students
+----------+-----------+ | name | marks | +----------+-----------+ | Rohit | 62 | | Rahul | 75 | | Inder | 99 | | Khushi | 49 | | Karan | 92 | +----------+-----------+
We want to find the names of the students which start with ‘K’.
Example
import mysql.connector db=mysql.connector.connect(host="your host", user="your username", password="your password",database="database_name") cursor=db.cursor() query="SELECT name FROM Students WHERE name LIKE 'K%' " cursor.execute(query) names=cursor.fetchall() for x in names: print(x) db.close()
The above code fetches all the names from the table which start with ‘K’.
Output
Karan Khushi
Similary, values can be serached based on various patterns using the above wildcards in any order.
- Related Articles
- What is the use of SOUNDS LIKE operator in MySQL?
- What is the use of MySQL NOT LIKE operator?
- What is the use of MySQL SOUNDS LIKE operator?
- Explain the use of AVG() function in MySQL using Python?
- Explain the use of SELECT DISTINCT statement in MySQL using Python?
- Explain the use of MIN() and MAX() using MySQL in Python?
- Explain the use of COUNT() AND SUM() in MySQL using Python?
- Explain and show the use of UNION in MySQL using Python?
- Explain function of % operator in Python.
- In MySQL, how does the precedence of ! operator in comparison with NOT operator depends upon HIGH_NOT_PRECEDENCE SQL mode?
- How to use MySQL SOUNDEX() function with LIKE operator to retrieve the records from table?
- What is the use of RLIKE operator in MySQL?
- Using ! operator in MySQL
- How Are MySQL INSTR() and LIKE operator similar?
- Display specific table names with MySQL LIKE Operator
