Python MySQL Bestel op
Sorteer het resultaat
Gebruik de instructie ORDER BY om het resultaat in oplopende of aflopende volgorde te sorteren.
Het trefwoord ORDER BY sorteert het resultaat standaard oplopend. Gebruik het trefwoord DESC om het resultaat in aflopende volgorde te sorteren.
Voorbeeld
Sorteer het resultaat alfabetisch op naam: resultaat:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor =
mydb.cursor()
sql = "SELECT * FROM customers ORDER BY name"
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
BESTELLEN OP OMSCHRIJVING
Gebruik het sleutelwoord DESC om het resultaat in aflopende volgorde te sorteren.
Voorbeeld
Sorteer het resultaat omgekeerd alfabetisch op naam:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "SELECT * FROM customers ORDER BY
name DESC"
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)