Saturday, March 18, 2023
HomeSoftware EngineeringHow you can ORDER BY a MySQL Question in Python

How you can ORDER BY a MySQL Question in Python


First, you have to the mysql.connector. If you’re uncertain of the right way to get this setup, seek advice from How you can Set up MySQL Driver in Python.

How you can Type the Results of a MySQL Question in Python

import mysql.connector

mydb = mysql.connector.join(
  host = "localhost",
  person = "username",
  password = "YoUrPaSsWoRd",
  database = "your_database"
)

mycursor = mydb.cursor()
sql = "SELECT * FROM clients ORDER BY identify"
mycursor.execute(sql)

myresult = mycursor.fetchall()

for x in myresult:
  print(x)

How you can Change Route of MySQL Order in Python

You may change the order of the type by merely setting the order route.

By default, it’s set to ORDER BY <column_name> ASC, which units the response in ascending order.

If you wish to change it, you merely substitute the ASC with DESC as follows:

import mysql.connector

mydb = mysql.connector.join(
  host = "localhost",
  person = "username",
  password = "YoUrPaSsWoRd",
  database = "your_database"
)

mycursor = mydb.cursor()
sql = "SELECT * FROM clients ORDER BY identify DESC"
mycursor.execute(sql)

myresult = mycursor.fetchall()

for x in myresult:
  print(x)
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments