import mysql.connector
# Establish a connection to the database
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
# Create a cursor object to execute SQL queries
mycursor = mydb.cursor()
# SQL query to select all records from the "customers" table and join the "orders" table using a left join
sql = "SELECT customers.name AS customer_name, orders.product_name AS order_name FROM customers LEFT JOIN orders ON customers.id = orders.customer_id"
# Execute the query
mycursor.execute(sql)
# Fetch all the rows returned by the query
result = mycursor.fetchall()
# Display the result
for row in result:
print(row)