Python PostgreSQL – 排序方式
在本文中,我们将讨论如何使用Python在 PostgreSQL 中使用 order by 子句。
默认情况下,Order By 子句用于对 SELECT 子句返回的表的记录进行升序排序,但也可以使用asc关键字。如果我们想按降序对记录进行排序,那么我们必须写desc字。
句法 :
SELECT
column1, column2, ....
FROM
table_name
ORDER BY
column1, colum2,.... [ASC | DESC]
使用中的数据:
首先,首先将所有必需的库导入工作空间,然后建立与数据库的连接。现在初始化一个游标并传递要执行的 SQL 语句。打印生成的结果集并关闭连接。
示例 1:以降序显示状态名称的Python代码
Python3
# importing psycopg2 module
import psycopg2
# establishing the connection
conn = psycopg2.connect(
database="postgres",
user='postgres',
password='password',
host='localhost',
port='5432'
)
# creating cursor object
cursor = conn.cursor()
# creating table
sql = '''CREATE TABLE Geeks(
id SERIAL NOT NULL,
name varchar(20) not null,
state varchar(20) not null
)'''
cursor.execute(sql)
# inserting values in the table
cursor.execute('''INSERT INTO Geeks(name , state) VALUES ('Babita','Bihar')''')
cursor.execute(
'''INSERT INTO Geeks(name , state) VALUES ('Anushka','Hyderabad')''')
cursor.execute(
'''INSERT INTO Geeks(name , state) VALUES ('Anamika','Banglore')''')
cursor.execute('''INSERT INTO Geeks(name , state) VALUES ('Sanaya','Pune')''')
cursor.execute(
'''INSERT INTO Geeks(name , state) VALUES ('Radha','Chandigarh')''')
# query to sort table by descending order of state
sql2 = 'select * from Geeks order by state desc;'
# executing query
cursor.execute(sql2)
# fetching records
print(cursor.fetchall())
# Commit your changes in the database
conn.commit()
# Closing the connection
conn.close()
Python3
# importing psycopg2 module
import psycopg2
# establishing the connection
conn = psycopg2.connect(
database="postgres",
user='postgres',
password='password',
host='localhost',
port='5432'
)
# creating cursor object
cursor = conn.cursor()
# creating table
sql = '''CREATE TABLE Geeks(
id SERIAL NOT NULL,
name varchar(20) not null,
state varchar(20) not null
)'''
cursor.execute(sql)
# inserting values in the table
cursor.execute('''INSERT INTO Geeks(name , state) VALUES ('Babita','Bihar')''')
cursor.execute(
'''INSERT INTO Geeks(name , state) VALUES ('Anushka','Hyderabad')''')
cursor.execute(
'''INSERT INTO Geeks(name , state) VALUES ('Anamika','Banglore')''')
cursor.execute('''INSERT INTO Geeks(name , state) VALUES ('Sanaya','Pune')''')
cursor.execute(
'''INSERT INTO Geeks(name , state) VALUES ('Radha','Chandigarh')''')
# query to sort table by name
sql2 = 'select * from Geeks order by name;'
# executing query
cursor.execute(sql2)
# fetching records
print(cursor.fetchall())
# Commit your changes in the database
conn.commit()
# Closing the connection
conn.close()
输出:
[(4, ‘Sanaya’, ‘Pune’), (2, ‘Anushka’, ‘Hyderabad’), (5, ‘Radha’, ‘Chandigarh’), (1, ‘Babita’, ‘Bihar’), (3, ‘Anamika’, ‘Banglore’)]
示例2:按姓名升序显示极客记录的Python代码
蟒蛇3
# importing psycopg2 module
import psycopg2
# establishing the connection
conn = psycopg2.connect(
database="postgres",
user='postgres',
password='password',
host='localhost',
port='5432'
)
# creating cursor object
cursor = conn.cursor()
# creating table
sql = '''CREATE TABLE Geeks(
id SERIAL NOT NULL,
name varchar(20) not null,
state varchar(20) not null
)'''
cursor.execute(sql)
# inserting values in the table
cursor.execute('''INSERT INTO Geeks(name , state) VALUES ('Babita','Bihar')''')
cursor.execute(
'''INSERT INTO Geeks(name , state) VALUES ('Anushka','Hyderabad')''')
cursor.execute(
'''INSERT INTO Geeks(name , state) VALUES ('Anamika','Banglore')''')
cursor.execute('''INSERT INTO Geeks(name , state) VALUES ('Sanaya','Pune')''')
cursor.execute(
'''INSERT INTO Geeks(name , state) VALUES ('Radha','Chandigarh')''')
# query to sort table by name
sql2 = 'select * from Geeks order by name;'
# executing query
cursor.execute(sql2)
# fetching records
print(cursor.fetchall())
# Commit your changes in the database
conn.commit()
# Closing the connection
conn.close()
输出:
[(3, ‘Anamika’, ‘Banglore’), (2, ‘Anushka’, ‘Hyderabad’), (1, ‘Babita’, ‘Bihar’), (5, ‘Radha’, ‘Chandigarh’), (4, ‘Sanaya’, ‘Pune’)]