codingstreets
Search
Close this search box.

Get Started: Python MySQL Where

python-mysql-where

In This Article, You Will Know About Python MySQL Where.

Python MySQL Where – Before moving ahead, let’s know a bit about Python MySQL Select From.

Table of Contents

Select With a Filter

Use ‘WHERE’ statement to filter the selection from a table.

Example: Use ‘WHERE’ statement to filter the table and return specified row.

				
					import mysql.connector
my_user_details = mysql.connector.connect(host="localhost",
                                          username="your_MySQL_username",
                                          password="Your_MySQL_password",
                                          database="YOUR_MYSQL_DATABASE_NAME")
database_for_mysql = my_user_details.cursor()

database_for_mysql.execute("SELECT * FROM Stu_information WHERE roll_no = 5204")
for result in database_for_mysql:
  print(result)
				
			
python-mysql-where

As a result, it returned the whole row related to roll_no 5204 from the all columns. 

Example: Use ‘WHERE’ statement to filter the table and return specified row. 

				
					import mysql.connector
my_user_details = mysql.connector.connect(host="localhost",
                                          username="your_MySQL_username",
                                          password="Your_MySQL_password",
                                          database="YOUR_MYSQL_DATABASE_NAME")
database_for_mysql = my_user_details.cursor()

database_for_mysql.execute("SELECT class FROM Stu_information WHERE roll_no = 5204")
for result in database_for_mysql:
  print(result)
				
			
python-mysql-where

As a result, it returned to the class from the only column related to roll_no 5204.

Wildcard Characters

You can select table with % allows to filter according to a phrase, letter, or number when it begins with, is ended with, and also includes.

The LIKE clause is used to compare a value to similar values using wildcard operators.

Example: Use % statement to filter the table with given letter “ay”.

				
					import mysql.connector
my_user_details = mysql.connector.connect(host="localhost",
                                          username="your_MySQL_username",
                                          password="Your_MySQL_password",
                                          database="YOUR_MYSQL_DATABASE_NAME")
database_for_mysql = my_user_details.cursor()

database_for_mysql.execute("SELECT * FROM Stu_information WHERE name LIKE '%ay%'")
for result in database_for_mysql:
  print(result)

				
			
python-mysql-where

As a result, it returned rows related to letters containing “ay” from all name’s columns.

Example: Returns [] when Wildcard characters not found with given number.

				
					import mysql.connector
my_user_details = mysql.connector.connect(host="localhost",
                                          username="your_MySQL_username",
                                          password="Your_MySQL_password",
                                          database="YOUR_MYSQL_DATABASE_NAME")
database_for_mysql = my_user_details.cursor()

database_for_mysql.execute("SELECT * FROM Stu_information WHERE name LIKE '%5204%'")
for result in database_for_mysql:
  print(result)

				
			

As a result, it returned blank list [] because given number did not match from column “name”.

If you find anything incorrect in the above-discussed topic and have further questions, please comment below.

Connect on:

Recent Articles