Home >>Python Tutorial >Python MySQL Order By
import mysql.connector mydb = mysql.connector.connect ( host=“localhost” , user=“yourusername” , passwd=“yourpassword” , database=“mydatabase” , ) mycursor = mydb.cursor ( ) sql = “SELECT * FROM customer ORDER BY name ” mycursor.execute ( sql ) myresult = mycursor.fetchall ( ) for x in myresult : print ( x )You can also sort the results in a mysql order by desc manner.
import mysql.connector mydb = mysql.connector.connect ( host=“localhost” , user=“yourusername” , passwd=“yourpassword” , database=“mydatabase” , ) mycursor = mydb.cursor ( ) sql = “SELECT * FROM customer ORDER BY name DESC ” mycursor.execute ( sql ) myresult = mycursor.fetchall ( ) for x in myresult : print ( x )With this, we finish the python sql order by part of our Python MySQL tutorial.