
- Python 資料訪問教程
- Python 資料訪問 - 首頁
- Python MySQL
- Python MySQL - 簡介
- Python MySQL - 資料庫連線
- Python MySQL - 建立資料庫
- Python MySQL - 建立表
- Python MySQL - 插入資料
- Python MySQL - 查詢資料 (Select)
- Python MySQL - WHERE 子句
- Python MySQL - 排序 (Order By)
- Python MySQL - 更新表
- Python MySQL - 刪除資料
- Python MySQL - 刪除表 (Drop Table)
- Python MySQL - LIMIT 子句
- Python MySQL - JOIN 操作
- Python MySQL - 遊標物件 (Cursor Object)
- Python PostgreSQL
- Python PostgreSQL - 簡介
- Python PostgreSQL - 資料庫連線
- Python PostgreSQL - 建立資料庫
- Python PostgreSQL - 建立表
- Python PostgreSQL - 插入資料
- Python PostgreSQL - 查詢資料 (Select)
- Python PostgreSQL - WHERE 子句
- Python PostgreSQL - 排序 (Order By)
- Python PostgreSQL - 更新表
- Python PostgreSQL - 刪除資料
- Python PostgreSQL - 刪除表 (Drop Table)
- Python PostgreSQL - LIMIT 子句
- Python PostgreSQL - JOIN 操作
- Python PostgreSQL - 遊標物件 (Cursor Object)
- Python SQLite
- Python SQLite - 簡介
- Python SQLite - 建立連線
- Python SQLite - 建立表
- Python SQLite - 插入資料
- Python SQLite - 查詢資料 (Select)
- Python SQLite - WHERE 子句
- Python SQLite - 排序 (Order By)
- Python SQLite - 更新表
- Python SQLite - 刪除資料
- Python SQLite - 刪除表 (Drop Table)
- Python SQLite - LIMIT 子句
- Python SQLite - JOIN 操作
- Python SQLite - 遊標物件 (Cursor Object)
- 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 PostgreSQL - 排序 (Order By)
通常情況下,如果您嘗試從表中檢索資料,您將按照插入資料的順序獲取記錄。
使用ORDER BY 子句,在檢索表記錄時,您可以根據所需的列按升序或降序對結果記錄進行排序。
語法
以下是 PostgreSQL 中 ORDER BY 子句的語法。
SELECT column-list FROM table_name [WHERE condition] [ORDER BY column1, column2, .. columnN] [ASC | DESC];
示例
假設我們使用以下查詢建立了一個名為 CRICKETERS 的表:
postgres=# CREATE TABLE CRICKETERS ( First_Name VARCHAR(255), Last_Name VARCHAR(255), Age int, Place_Of_Birth VARCHAR(255), Country VARCHAR(255) ); CREATE TABLE postgres=#
如果我們使用 INSERT 語句向其中插入了 5 條記錄:
postgres=# insert into CRICKETERS values('Shikhar', 'Dhawan', 33, 'Delhi', 'India'); INSERT 0 1 postgres=# insert into CRICKETERS values('Jonathan', 'Trott', 38, 'CapeTown', 'SouthAfrica'); INSERT 0 1 postgres=# insert into CRICKETERS values('Kumara', 'Sangakkara', 41, 'Matale', 'Srilanka'); INSERT 0 1 postgres=# insert into CRICKETERS values('Virat', 'Kohli', 30, 'Delhi', 'India'); INSERT 0 1 postgres=# insert into CRICKETERS values('Rohit', 'Sharma', 32, 'Nagpur', 'India'); INSERT 0 1
以下 SELECT 語句按年齡升序檢索 CRICKETERS 表的行:
postgres=# SELECT * FROM CRICKETERS ORDER BY AGE; first_name | last_name | age | place_of_birth | country ------------+------------+-----+----------------+------------- Virat | Kohli | 30 | Delhi | India Rohit | Sharma | 32 | Nagpur | India Shikhar | Dhawan | 33 | Delhi | India Jonathan | Trott | 38 | CapeTown | SouthAfrica Kumara | Sangakkara | 41 | Matale | Srilanka (5 rows)es:
您可以使用多個列對錶的記錄進行排序。以下 SELECT 語句根據 age 和 FIRST_NAME 列對 CRICKETERS 表的記錄進行排序。
postgres=# SELECT * FROM CRICKETERS ORDER BY AGE, FIRST_NAME; first_name | last_name | age | place_of_birth | country ------------+------------+-----+----------------+------------- Virat | Kohli | 30 | Delhi | India Rohit | Sharma | 32 | Nagpur | India Shikhar | Dhawan | 33 | Delhi | India Jonathan | Trott | 38 | CapeTown | SouthAfrica Kumara | Sangakkara | 41 | Matale | Srilanka (5 rows)
預設情況下,ORDER BY 子句按升序對錶記錄進行排序。您可以使用 DESC 將結果按降序排列:
postgres=# SELECT * FROM CRICKETERS ORDER BY AGE DESC; first_name | last_name | age | place_of_birth | country ------------+------------+-----+----------------+------------- Kumara | Sangakkara | 41 | Matale | Srilanka Jonathan | Trott | 38 | CapeTown | SouthAfrica Shikhar | Dhawan | 33 | Delhi | India Rohit | Sharma | 32 | Nagpur | India Virat | Kohli | 30 | Delhi | India (5 rows)
使用 Python 的 ORDER BY 子句
要按特定順序檢索表的內容,請在遊標物件上呼叫 execute() 方法,並將帶有 ORDER BY 子句的 SELECT 語句作為引數傳遞給它。
示例
在下面的示例中,我們建立一個名為 Employee 的表,填充它,並使用 ORDER BY 子句按年齡(升序)檢索其記錄。
import psycopg2 #establishing the connection conn = psycopg2.connect( database="mydb", user='postgres', password='password', host='127.0.0.1', port= '5432' ) #Setting auto commit false conn.autocommit = True #Creating a cursor object using the cursor() method cursor = conn.cursor() #Doping EMPLOYEE table if already exists. cursor.execute("DROP TABLE IF EXISTS EMPLOYEE") #Creating a table sql = '''CREATE TABLE EMPLOYEE( FIRST_NAME CHAR(20) NOT NULL, LAST_NAME CHAR(20), AGE INT, SEX CHAR(1), INCOME INT, CONTACT INT )''' cursor.execute(sql) #Populating the table insert_stmt = "INSERT INTO EMPLOYEE ( FIRST_NAME, LAST_NAME, AGE, SEX, INCOME, CONTACT) VALUES (%s, %s, %s, %s, %s, %s)" data = [('Krishna', 'Sharma', 26, 'M', 2000, 101), ('Raj', 'Kandukuri', 20, 'M', 7000, 102), ('Ramya', 'Ramapriya', 29, 'F', 5000, 103), ('Mac', 'Mohan', 26, 'M', 2000, 104)] 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()) #Commit your changes in the database conn.commit() #Closing the connection conn.close()
輸出
[('Sharukh', 'Sheik', 25, 'M', 8300.0), ('Sarmista', 'Sharma', 26, 'F', 10000.0)]
廣告