Python Database Ordering Result
The ORDER BY clause is used to order the result. Consider the following example.
Python Database Ordering Result Examples
import mysql.connector myconn = mysql.connector.connect(host = "localhost", user = "root",password = "naveen",database = "PythonDB") cur = myconn.cursor() try: cur.execute("select name, id, salary from Employee order by name") result = cur.fetchall() print("Name id Salary"); for row in result: print("%s\t%d\t%d"%(row[0],row[1],row[2])) except: myconn.rollback() myconn.close()
Order By Name and descending Order
This orders the result in the decreasing order of a particular column.
import mysql.connector myconn = mysql.connector.connect(host = "localhost", user = "root",password = "naveen",database = "PythonDB") cur = myconn.cursor() try: cur.execute("select name, id, salary from Employee order by name desc") result = cur.fetchall() print("Name id Salary"); for row in result: print("%s\t%d\t%d"%(row[0],row[1],row[2])) except: myconn.rollback() myconn.close()