📜  Python SQLite – ORDER BY 子句

📅  最后修改于: 2022-05-13 01:54:29.973000             🧑  作者: Mango

Python SQLite – ORDER BY 子句

在本文中,我们将使用Python讨论 SQLite 中的ORDER BY子句。 ORDER BY 语句是一种 SQL 语句,用于根据一个或多个列按升序或降序对数据进行排序。默认情况下,ORDER BY 按升序对数据进行排序。

  • DESC用于按降序对数据进行排序。
  • ASC按升序排序。

首先,让我们创建一个数据库。

Python3
# importing sqlite module
import sqlite3
  
# create connection to the database 
# geeks_database
connection = sqlite3.connect('geeks_database.db')
  
# create table named address of customers 
# with 4 columns id,name age and address
connection.execute('''CREATE TABLE customer_address
         (ID INT PRIMARY KEY     NOT NULL,
         NAME           TEXT    NOT NULL,
         AGE            INT     NOT NULL,
         ADDRESS        CHAR(50)); ''')
  
# close the connection
connection.close()


Python3
# importing sqlite module
import sqlite3
  
# create connection to the database 
# geeks_database
connection = sqlite3.connect('geeks_database.db')
  
# insert records into table
connection.execute(
    "INSERT INTO customer_address VALUES (1, 'nikhil teja', 22, 'hyderabad' )")
connection.execute(
    "INSERT INTO customer_address VALUES (2, 'karthik', 25, 'khammam')")
connection.execute(
    "INSERT INTO customer_address VALUES (3, 'sravan', 22, 'ponnur' )")
connection.execute(
    "INSERT INTO customer_address VALUES (4, 'deepika', 25, 'chebrolu' )")
connection.execute(
    "INSERT INTO customer_address VALUES (5, 'jyothika', 22, 'noida')")
  
# close the connection
connection.close()


Python3
# importing sqlite module
import sqlite3
  
# create connection to the database
# geeks_database
connection = sqlite3.connect('geeks_database.db')
  
# sql query to display all details from 
# table in ascending order based on address.
cursor = connection.execute(
    "SELECT ADDRESS,ID from customer_address ORDER BY address DESC")
  
# display data row by row
for i in cursor:
    print(i)
  
# close the connection
connection.close()


Python3
# importing sqlite module
import sqlite3
  
# create connection to the database 
# geeks_database
connection = sqlite3.connect('geeks_database.db')
  
# sql query to display address and id
# based on address in descending order
cursor = connection.execute(
    "SELECT ADDRESS,ID from customer_address ORDER BY address DESC")
  
# display data row by row
for i in cursor:
    print(i)
  
# close the connection
connection.close()


Python3
# importing sqlite module
import sqlite3
  
# create connection to the database 
# geeks_database
connection = sqlite3.connect('geeks_database.db')
  
# sql query to display name and id based
# on name in descending order
cursor = connection.execute(
    "SELECT NAME,ID from customer_address ORDER BY NAME DESC")
  
# display data row by row
for i in cursor:
    print(i)
  
# close the connection
connection.close()


输出:



现在,将 5 条记录插入到 customer_address 表中。

蟒蛇3

# importing sqlite module
import sqlite3
  
# create connection to the database 
# geeks_database
connection = sqlite3.connect('geeks_database.db')
  
# insert records into table
connection.execute(
    "INSERT INTO customer_address VALUES (1, 'nikhil teja', 22, 'hyderabad' )")
connection.execute(
    "INSERT INTO customer_address VALUES (2, 'karthik', 25, 'khammam')")
connection.execute(
    "INSERT INTO customer_address VALUES (3, 'sravan', 22, 'ponnur' )")
connection.execute(
    "INSERT INTO customer_address VALUES (4, 'deepika', 25, 'chebrolu' )")
connection.execute(
    "INSERT INTO customer_address VALUES (5, 'jyothika', 22, 'noida')")
  
# close the connection
connection.close()

输出:

创建数据库并向其中添加数据后,让我们看看 order by 子句的使用。

示例 1:根据地址按升序(默认)显示表中的所有详细信息。

蟒蛇3



# importing sqlite module
import sqlite3
  
# create connection to the database
# geeks_database
connection = sqlite3.connect('geeks_database.db')
  
# sql query to display all details from 
# table in ascending order based on address.
cursor = connection.execute(
    "SELECT ADDRESS,ID from customer_address ORDER BY address DESC")
  
# display data row by row
for i in cursor:
    print(i)
  
# close the connection
connection.close()

输出:

示例2:根据地址降序显示地址和id。

蟒蛇3

# importing sqlite module
import sqlite3
  
# create connection to the database 
# geeks_database
connection = sqlite3.connect('geeks_database.db')
  
# sql query to display address and id
# based on address in descending order
cursor = connection.execute(
    "SELECT ADDRESS,ID from customer_address ORDER BY address DESC")
  
# display data row by row
for i in cursor:
    print(i)
  
# close the connection
connection.close()

输出:

示例 3:根据名称按降序显示名称和 id

蟒蛇3

# importing sqlite module
import sqlite3
  
# create connection to the database 
# geeks_database
connection = sqlite3.connect('geeks_database.db')
  
# sql query to display name and id based
# on name in descending order
cursor = connection.execute(
    "SELECT NAME,ID from customer_address ORDER BY NAME DESC")
  
# display data row by row
for i in cursor:
    print(i)
  
# close the connection
connection.close()

输出: