-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathw3school_pythonmysqlorderby.py
More file actions
45 lines (28 loc) · 964 Bytes
/
w3school_pythonmysqlorderby.py
File metadata and controls
45 lines (28 loc) · 964 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Python MySQL Order By
# Sort the Result
# Use the "ORDER BY" statement to sort the result in ascending or descending order.
# The "ORDER BY" keyword sorts the result ascending by default. To sort the result in descending order, use the "DESC" keyword.
# Example - Sort the result alphabetically by name: result:
import mysql.connector # import MySQL Connector
mydb = mysql.connector.connect(
host = "localhost",
user = "root",
password = "mahanta1",
database = "mydatabase"
)
mycursor = mydb.cursor()
"""
sql = "SELECT * FROM customers ORDER BY name"
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
"""
# Order By DESC
# Use the "DESC" keyword to sort the result in a descending order.
# Example - Sort the result reverse alphabetically by name:
sql = "SELECT * FROM customers ORDER BY name DESC"
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)