How to Use IF Statement in MySQL Using Python Last Updated : 03 Jan, 2021 Comments Improve Suggest changes Like Article Like Report Prerequisite: Python: MySQL Create Table In this article, we are going to see how to use if statements in MySQL using Python. Python allows the integration of a wide range of database servers with applications. A database interface is required to access a database from Python. MySQL Connector-Python module is an API in python for communicating with a MySQL database. Approach: Import module.Make a connection request with the database.Create an object for the database cursor.Execute the following MySQL query:IF(condition, value_if_true, value_if_false) Example 1: In this example we are using this database table with the following query; Below is the implementation: Python3 # Establish connection to MySQL database import mysql.connector db = mysql.connector.connect( host="localhost", user="root", password="root123", database = "geeks" ) #getting the cursor by cursor() method mycursor = db.cursor() insertQuery = " Select Value, IF(Value>1000,'MORE','LESS') from salary;" mycursor.execute(insertQuery) myresult = mycursor.fetchall() print(myresult) # close the Connection db.close() Output: Example 2: In this example we are using this database table with the following query; Below is the full implementation: Python3 # Establish connection to MySQL database import mysql.connector db = mysql.connector.connect( host="localhost", user="root", password="root123", database = "geeks" ) #getting the cursor by cursor() method mycursor = db.cursor() insertQuery = " Select City, IF(City = 'Patna','True','False') from persons;" mycursor.execute(insertQuery) myresult = mycursor.fetchall() print(myresult) # close the Connection db.close() Output: Comment More infoAdvertise with us Next Article How to Use IF Statement in MySQL Using Python kumar_satyam Follow Improve Article Tags : Python Python-mySQL Practice Tags : python Similar Reads How to Copy a Table in MySQL Using Python? In this article, we will create a table in MySQL and will create a copy of that table using Python. We will copy the entire table, including all the columns and the definition of the columns, as well as all rows of data in the table. To connect to MySQL database using python, we need PyMySql module. 3 min read How to Show All Tables in MySQL using Python? A connector is employed when we have to use mysql with other programming languages. The work of mysql-connector is to provide access to MySQL Driver to the required language. Thus, it generates a connection between the programming language and the MySQL Server. In order to make python interact with 1 min read Nested-if statement in Python For more complex decision trees, Python allows for nested if statements where one if statement is placed inside another. This article will explore the concept of nested if statements in Python, providing clarity on how to use them effectively.Python Nested if StatementA nested if statement in Python 2 min read Rename column name in MySQL using Python In this article, we are going to see how we can rename or alter the name of a column table using Python. Python allows the integration of a wide range of database servers with applications. A database interface is required to access a database from Python. MySQL Connector-Python module is an API in 1 min read Export Data From Mysql to Excel Sheet Using Python We are given a MySQL database and our task is to export the data into an excel sheet using Python. In this article, we will see how to export data from MySQL to Excel Sheets using Python. Export Data From Mysql to Excel Sheet Using PythonBelow are some of the ways by which we can export data from My 3 min read Like