Python

Sort the result in python using mysql

Pinterest LinkedIn Tumblr

Use the ORDER BY statement to sort the result in ascending order.


import mysql.connector as mysql

db = mysql.connect(
    host ="localhost",
    username= "root",
    password = "root@123",
    database = "realprogrammers"
)

cursor = db.cursor()

# defining the query

query = "SELECT * FROM user ORDER BY user_name"

cursor.execute(query)

result = cursor.fetchall()

for x in result:
  print(x)

Fetch data only column


import mysql.connector as mysql

db = mysql.connect(
    host ="localhost",
    username= "root",
    password = "root@123",
    database = "realprogrammers"
)

cursor = db.cursor()

# defining the query

query = "SELECT * FROM user ORDER BY user_name"

cursor.execute(query)

result = cursor.fetchall()

for x in result:
  print(x[0])

Write A Comment