
- Python PostgreSQL 教程
- 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 - 限制
- Python PostgreSQL - 聯合
- Python PostgreSQL - 遊標物件
- Python PostgreSQL 實用資源
- Python PostgreSQL - 快速指南
- Python PostgreSQL - 實用資源
- Python PostgreSQL - 討論
Python PostgreSQL - Where 子句
在執行 SELECT、UPDATE 或 DELETE 操作時,您可以使用 WHERE 子句指定條件以篩選記錄。操作將基於滿足給定條件的記錄來執行。
語法
以下是 PostgreSQL 中 WHERE 子句的語法 −
SELECT column1, column2, columnN FROM table_name WHERE [search_condition]
您可以使用比較或邏輯運算子(如 >、<、=、LIKE、NOT 等)指定 search_condition。以下示例將闡明這個概念。
示例
假設我們使用以下查詢建立了一個名為 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 語句檢索年齡大於 35 的記錄 −
postgres=# SELECT * FROM CRICKETERS WHERE AGE > 35; first_name | last_name | age | place_of_birth | country ------------+------------+-----+----------------+------------- Jonathan | Trott | 38 | CapeTown | SouthAfrica Kumara | Sangakkara | 41 | Matale | Srilanka (2 rows) postgres=#
使用 Python 的 Where 子句
要使用 python 程式從表格中獲取特定記錄,請透過將它作為一個引數傳遞給 execute() 方法,使用帶有 WHERE 子句的 SELECT 語句。
示例
以下 python 示例演示瞭如何使用 python 中的 WHERE 命令。
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") 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', 19, 'M', 2000), ('Raj', 'Kandukuri', 20, 'M', 7000), ('Ramya', 'Ramapriya', 25, 'M', 5000),('Mac', 'Mohan', 26, 'M', 2000)] cursor.executemany(insert_stmt, data) #Retrieving specific records using the where clause cursor.execute("SELECT * from EMPLOYEE WHERE AGE <23") print(cursor.fetchall()) #Commit your changes in the database conn.commit() #Closing the connection conn.close()
輸出
[('Krishna', 'Sharma', 19, 'M', 2000.0), ('Raj', 'Kandukuri', 20, 'M', 7000.0)]
廣告