右联接(RIGHT JOIN)

  1. import mysql.connector
  2.  
  3. mydb = mysql.connector.connect(
  4. host="localhost",
  5. user="myusername",
  6. passwd="mypassword",
  7. database="mydatabase"
  8. )
  9.  
  10. mycursor = mydb.cursor()
  11.  
  12. sql = "SELECT \
  13. users.name AS user, \
  14. products.name AS favorite \
  15. FROM users \
  16. RIGHT JOIN products ON users.fav = products.id"
  17.  
  18. mycursor.execute(sql)
  19.  
  20. myresult = mycursor.fetchall()
  21.  
  22. for x in myresult:
  23. print(x)