import mysql.connector
# Connect 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 join two tables
sql = "SELECT customers.name, orders.product_name FROM customers JOIN orders ON customers.id = orders.customer_id"
# Execute the query
mycursor.execute(sql)
# Fetch all the rows
result = mycursor.fetchall()
# Display the result
for row in result:
print(row)