📜  SQLAlchemy – 聚合函数

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

SQLAlchemy – 聚合函数

在本文中,我们将看到如何使用 SQLAlchemy 和Python选择行数。

在开始之前,让我们使用 pip 安装所需的依赖项:

pip install sqlalchemy

由于我们将在这篇文章中使用 MySQL,因此我们还将在Python中为 MySQL 安装 SQL 连接器。但是,除了 SQL 连接器之外,没有任何代码实现会随着数据库的变化而变化。

pip install pymysql

常用的 SQL 聚合函数有 5 个,如下所示:

SQL 聚合函数

在本文中,我们将介绍上述每个聚合函数的示例。在这两个示例中,我们将计算sakila数据库中支付表中存在的记录数。付款表中的示例记录如下所示:

付款表

如果您没有sakila数据库,并且想在不安装它的情况下继续阅读本文,则使用下面提到的链接中的 SQL 脚本来创建所需的架构和付款表以及记录。 Sakila 付款表脚本

SQLAlchemy 中的func函数用于实现这些聚合函数。下表总结了用于每个聚合函数的方法。

SQL Aggregate FunctionSQLAlchemy Method 
MIN()sqlalchemy.func.min()
MAX()sqlalchemy.func.max()
SUM()sqlalchemy.func.sum()
AVG()sqlalchemy.func.avg()
COUNT()sqlalchemy.func.count()

最小()

在这里,我们将使用 sqlalchemy.func.min()函数来获取行的最小元素。

代码:

Python
# IMPORT THE REQUIRED LIBRARY
import sqlalchemy as db
 
# DEFINE THE ENGINE (CONNECTION OBJECT)
engine = db.create_engine("mysql+pymysql://\
root:password@localhost/sakila")
 
# CREATE THE METADATA OBJECT TO ACCESS THE TABLE
meta_data = db.MetaData(bind=engine)
db.MetaData.reflect(meta_data)
 
# GET THE `payment` TABLE FROM THE METADATA OBJECT
payment_table = meta_data.tables['payment']
 
# SELECT MIN(amount) FROM sakila.`payment`;
query = db.select([db.func.min(payment_table.c.amount)])
 
# FETCH ALL THE RECORDS IN THE RESPONSE
result = engine.execute(query).first()
 
# VIEW THE RESULT
print(result[0])


Python
# IMPORT THE REQUIRED LIBRARY
import sqlalchemy as db
 
# DEFINE THE ENGINE (CONNECTION OBJECT)
engine = db.create_engine("mysql+pymysql://\
root:password@localhost/sakila")
 
# CREATE THE METADATA OBJECT TO ACCESS THE TABLE
meta_data = db.MetaData(bind=engine)
db.MetaData.reflect(meta_data)
 
# GET THE `payment` TABLE FROM THE METADATA OBJECT
payment_table = meta_data.tables['payment']
 
# SELECT MAX(amount) FROM sakila.`payment`;
query = db.select([db.func.max(payment_table.c.amount)])
 
# FETCH ALL THE RECORDS IN THE RESPONSE
result = engine.execute(query).first()
 
# VIEW THE RESULT
print(result[0])


Python
# IMPORT THE REQUIRED LIBRARY
import sqlalchemy as db
 
# DEFINE THE ENGINE (CONNECTION OBJECT)
engine = db.create_engine("mysql+pymysql://\
root:password@localhost/sakila")
 
# CREATE THE METADATA OBJECT TO ACCESS THE TABLE
meta_data = db.MetaData(bind=engine)
db.MetaData.reflect(meta_data)
 
# GET THE `payment` TABLE FROM THE METADATA OBJECT
payment_table = meta_data.tables['payment']
 
# SELECT SUM(amount) FROM sakila.`payment`;
query = db.select([db.func.sum(payment_table.c.amount)])
 
# FETCH ALL THE RECORDS IN THE RESPONSE
result = engine.execute(query).first()
 
# VIEW THE RESULT
print(result[0])


Python
# IMPORT THE REQUIRED LIBRARY
import sqlalchemy as db
 
# DEFINE THE ENGINE (CONNECTION OBJECT)
engine = db.create_engine("mysql+pymysql://\
root:password@localhost/sakila")
 
# CREATE THE METADATA OBJECT TO ACCESS THE TABLE
meta_data = db.MetaData(bind=engine)
db.MetaData.reflect(meta_data)
 
# GET THE `payment` TABLE FROM THE METADATA OBJECT
payment_table = meta_data.tables['payment']
 
# SELECT AVG(amount) FROM sakila.`payment`;
query = db.select([db.func.avg(payment_table.c.amount)])
 
# FETCH ALL THE RECORDS IN THE RESPONSE
result = engine.execute(query).first()
 
# VIEW THE RESULT
print(result[0])


Python
# IMPORT THE REQUIRED LIBRARY
import sqlalchemy as db
 
# DEFINE THE ENGINE (CONNECTION OBJECT)
engine = db.create_engine("mysql+pymysql://\
root:password@localhost/sakila")
 
# CREATE THE METADATA OBJECT TO ACCESS THE TABLE
meta_data = db.MetaData(bind=engine)
db.MetaData.reflect(meta_data)
 
# GET THE `payment` TABLE FROM THE METADATA OBJECT
payment_table = meta_data.tables['payment']
 
# SELECT COUNT(amount) FROM sakila.`payment`;
query = db.select([db.func.count(payment_table.c.amount)])
 
# FETCH ALL THE RECORDS IN THE RESPONSE
result = engine.execute(query).first()
 
# VIEW THE RESULT
print(result[0])


输出:

0.00

最大限度()

在这里,我们将使用 sqlalchemy.func.max()函数来获取行的最大元素。

代码:

Python

# IMPORT THE REQUIRED LIBRARY
import sqlalchemy as db
 
# DEFINE THE ENGINE (CONNECTION OBJECT)
engine = db.create_engine("mysql+pymysql://\
root:password@localhost/sakila")
 
# CREATE THE METADATA OBJECT TO ACCESS THE TABLE
meta_data = db.MetaData(bind=engine)
db.MetaData.reflect(meta_data)
 
# GET THE `payment` TABLE FROM THE METADATA OBJECT
payment_table = meta_data.tables['payment']
 
# SELECT MAX(amount) FROM sakila.`payment`;
query = db.select([db.func.max(payment_table.c.amount)])
 
# FETCH ALL THE RECORDS IN THE RESPONSE
result = engine.execute(query).first()
 
# VIEW THE RESULT
print(result[0])

输出:

11.99

和()

在这里,我们将使用 sqlalchemy.func.sum()函数来获取行的总和元素。

代码:

Python

# IMPORT THE REQUIRED LIBRARY
import sqlalchemy as db
 
# DEFINE THE ENGINE (CONNECTION OBJECT)
engine = db.create_engine("mysql+pymysql://\
root:password@localhost/sakila")
 
# CREATE THE METADATA OBJECT TO ACCESS THE TABLE
meta_data = db.MetaData(bind=engine)
db.MetaData.reflect(meta_data)
 
# GET THE `payment` TABLE FROM THE METADATA OBJECT
payment_table = meta_data.tables['payment']
 
# SELECT SUM(amount) FROM sakila.`payment`;
query = db.select([db.func.sum(payment_table.c.amount)])
 
# FETCH ALL THE RECORDS IN THE RESPONSE
result = engine.execute(query).first()
 
# VIEW THE RESULT
print(result[0])

输出:

67416.51

平均()

在这里,我们将使用 sqlalchemy.func.avg()函数来获取行的平均元素。

代码:

Python

# IMPORT THE REQUIRED LIBRARY
import sqlalchemy as db
 
# DEFINE THE ENGINE (CONNECTION OBJECT)
engine = db.create_engine("mysql+pymysql://\
root:password@localhost/sakila")
 
# CREATE THE METADATA OBJECT TO ACCESS THE TABLE
meta_data = db.MetaData(bind=engine)
db.MetaData.reflect(meta_data)
 
# GET THE `payment` TABLE FROM THE METADATA OBJECT
payment_table = meta_data.tables['payment']
 
# SELECT AVG(amount) FROM sakila.`payment`;
query = db.select([db.func.avg(payment_table.c.amount)])
 
# FETCH ALL THE RECORDS IN THE RESPONSE
result = engine.execute(query).first()
 
# VIEW THE RESULT
print(result[0])

输出:

4.200667

数数()

在这里,我们将使用 sqlalchemy.func.count()函数来获取行数。

代码:

Python

# IMPORT THE REQUIRED LIBRARY
import sqlalchemy as db
 
# DEFINE THE ENGINE (CONNECTION OBJECT)
engine = db.create_engine("mysql+pymysql://\
root:password@localhost/sakila")
 
# CREATE THE METADATA OBJECT TO ACCESS THE TABLE
meta_data = db.MetaData(bind=engine)
db.MetaData.reflect(meta_data)
 
# GET THE `payment` TABLE FROM THE METADATA OBJECT
payment_table = meta_data.tables['payment']
 
# SELECT COUNT(amount) FROM sakila.`payment`;
query = db.select([db.func.count(payment_table.c.amount)])
 
# FETCH ALL THE RECORDS IN THE RESPONSE
result = engine.execute(query).first()
 
# VIEW THE RESULT
print(result[0])

输出:

16049