
- Python 資料訪問教程
- Python 資料訪問 - 首頁
- Python MySQL
- Python MySQL - 簡介
- Python MySQL - 資料庫連線
- Python MySQL - 建立資料庫
- Python MySQL - 建立表
- Python MySQL - 插入資料
- Python MySQL - 查詢資料
- Python MySQL - WHERE 子句
- Python MySQL - 排序
- Python MySQL - 更新表
- Python MySQL - 刪除資料
- Python MySQL - 刪除表
- Python MySQL - LIMIT
- Python MySQL - JOIN
- Python MySQL - 遊標物件
- Python PostgreSQL
- Python PostgreSQL - 簡介
- Python PostgreSQL - 資料庫連線
- Python PostgreSQL - 建立資料庫
- Python PostgreSQL - 建立表
- Python PostgreSQL - 插入資料
- Python PostgreSQL - 查詢資料
- Python PostgreSQL - WHERE 子句
- Python PostgreSQL - 排序
- Python PostgreSQL - 更新表
- Python PostgreSQL - 刪除資料
- Python PostgreSQL - 刪除表
- Python PostgreSQL - LIMIT
- Python PostgreSQL - JOIN
- Python PostgreSQL - 遊標物件
- Python SQLite
- Python SQLite - 簡介
- Python SQLite - 建立連線
- Python SQLite - 建立表
- Python SQLite - 插入資料
- Python SQLite - 查詢資料
- Python SQLite - WHERE 子句
- Python SQLite - 排序
- Python SQLite - 更新表
- Python SQLite - 刪除資料
- Python SQLite - 刪除表
- Python SQLite - LIMIT
- Python SQLite - JOIN
- Python SQLite - 遊標物件
- Python MongoDB
- Python MongoDB - 簡介
- Python MongoDB - 建立資料庫
- Python MongoDB - 建立集合
- Python MongoDB - 插入文件
- Python MongoDB - 查詢
- Python MongoDB - 查詢
- Python MongoDB - 排序
- Python MongoDB - 刪除文件
- Python MongoDB - 刪除集合
- Python MongoDB - 更新
- Python MongoDB - LIMIT
- Python 資料訪問資源
- Python 資料訪問 - 快速指南
- Python 資料訪問 - 有用資源
- Python 資料訪問 - 討論
Python MySQL - 排序
使用 SELECT 查詢獲取資料時,您可以使用 OrderBy 子句按所需順序(升序或降序)對結果進行排序。預設情況下,此子句按升序排序結果;如果您需要按降序排列,則需要顯式使用“DESC”。
語法
以下是語法 SELECT 列列表
FROM table_name [WHERE condition] [ORDER BY column1, column2,.. columnN] [ASC | DESC]; of the ORDER BY clause:
示例
假設我們在 MySQL 中建立了一個名為 EMPLOYEES 的表,如下所示:
mysql> CREATE TABLE EMPLOYEE( FIRST_NAME CHAR(20) NOT NULL, LAST_NAME CHAR(20), AGE INT, SEX CHAR(1), INCOME FLOAT ); Query OK, 0 rows affected (0.36 sec)
如果我們使用 INSERT 語句向其中插入了 4 條記錄,如下所示:
mysql> INSERT INTO EMPLOYEE VALUES ('Krishna', 'Sharma', 19, 'M', 2000), ('Raj', 'Kandukuri', 20, 'M', 7000), ('Ramya', 'Ramapriya', 25, 'F', 5000), ('Mac', 'Mohan', 26, 'M', 2000);
以下語句按年齡的升序檢索 EMPLOYEE 表的內容。
mysql> SELECT * FROM EMPLOYEE ORDER BY AGE; +------------+-----------+------+------+--------+ | FIRST_NAME | LAST_NAME | AGE | SEX | INCOME | +------------+-----------+------+------+--------+ | Krishna | Sharma | 19 | M | 2000 | | Raj | Kandukuri | 20 | M | 7000 | | Ramya | Ramapriya | 25 | F | 5000 | | Mac | Mohan | 26 | M | 2000 | +------------+-----------+------+------+--------+ 4 rows in set (0.04 sec)
您還可以使用 DESC 按降序檢索資料,如下所示:
mysql> SELECT * FROM EMPLOYEE ORDER BY FIRST_NAME, INCOME DESC; +------------+-----------+------+------+--------+ | FIRST_NAME | LAST_NAME | AGE | SEX | INCOME | +------------+-----------+------+------+--------+ | Krishna | Sharma | 19 | M | 2000 | | Mac | Mohan | 26 | M | 2000 | | Raj | Kandukuri | 20 | M | 7000 | | Ramya | Ramapriya | 25 | F | 5000 | +------------+-----------+------+------+--------+ 4 rows in set (0.00 sec)
使用 python 的 ORDER BY 子句
要按特定順序檢索表的內容,請在遊標物件上呼叫 **execute()** 方法,並將 SELECT 語句以及 ORDER BY 子句作為引數傳遞給它。
示例
在下面的示例中,我們建立一個名為 Employee 的表,填充它,並使用 ORDER BY 子句按年齡的(升序)順序檢索其記錄。
import mysql.connector #establishing the connection conn = mysql.connector.connect( user='root', password='password', host='127.0.0.1', database='mydb') #Creating a cursor object using the cursor() method cursor = conn.cursor() #Doping EMPLOYEE table if already exists. cursor.execute("DROP TABLE IF EXISTS EMPLOYEE") sql = '''CREATE TABLE EMPLOYEE( FIRST_NAME CHAR(20) NOT NULL, LAST_NAME CHAR(20), AGE INT, SEX CHAR(1), INCOME FLOAT )''' cursor.execute(sql) #Populating the table insert_stmt = "INSERT INTO EMPLOYEE (FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES (%s, %s, %s, %s, %s)" data = [('Krishna', 'Sharma', 26, 'M', 2000), ('Raj', 'Kandukuri', 20, 'M', 7000), ('Ramya', 'Ramapriya', 29, 'F', 5000), ('Mac', 'Mohan', 26, 'M', 2000)] cursor.executemany(insert_stmt, data) conn.commit() #Retrieving specific records using the ORDER BY clause cursor.execute("SELECT * from EMPLOYEE ORDER BY AGE") print(cursor.fetchall()) #Closing the connection conn.close()
輸出
[('Raj', 'Kandukuri', 20, 'M', 7000.0), ('Krishna', 'Sharma', 26, 'M', 2000.0), ('Mac', 'Mohan', 26, 'M', 2000.0), ('Ramya', 'Ramapriya', 29, 'F', 5000.0) ]
同樣,您可以使用 ORDER BY 子句按降序從表中檢索資料。
示例
import mysql.connector #establishing the connection conn = mysql.connector.connect( user='root', password='password', host='127.0.0.1', database='mydb') #Creating a cursor object using the cursor() method cursor = conn.cursor() #Retrieving specific records using the ORDERBY clause cursor.execute("SELECT * from EMPLOYEE ORDER BY INCOME DESC") print(cursor.fetchall()) #Closing the connection conn.close()
輸出
[('Raj', 'Kandukuri', 20, 'M', 7000.0), ('Ramya', 'Ramapriya', 29, 'F', 5000.0), ('Krishna', 'Sharma', 26, 'M', 2000.0), ('Mac', 'Mohan', 26, 'M', 2000.0) ]
廣告