Wednesday, March 22, 2023
HomeSoftware EngineeringFind out how to ORDER BY a MySQL Question in Python

Find out how to ORDER BY a MySQL Question in Python


First, you will want the mysql.connector. If you’re not sure of how one can get this setup, seek advice from Find out how to Set up MySQL Driver in Python.

Find out how to 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 prospects ORDER BY title"
mycursor.execute(sql)

myresult = mycursor.fetchall()

for x in myresult:
  print(x)

Find out how to Change Route of MySQL Order in Python

You’ll be able to change the order of the kind by merely setting the order course.

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 exchange 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 prospects ORDER BY title 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

thirteen − 11 =

Most Popular

Recent Comments