📜  如何使用过滤器获取 SQLAlchemy 中的特定列?

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

如何使用过滤器获取 SQLAlchemy 中的特定列?

在本文中,我们将了解如何在Python中使用 SQLAlchemy 查询和选择特定列。

对于我们的示例,我们已经创建了一个学生表,我们将使用它:

学生表

根据过滤器选择 SQLAlchemy 中的特定列:

  • 在 SQLAlchemy 中选择特定列
  • 在 SQLAlchemy 中过滤记录

示例 1:基于单个过滤器选择特定列

Python
import sqlalchemy as db
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
  
Base = declarative_base()
  
# DEFINE THE ENGINE (CONNECTION OBJECT)
engine = db.create_engine("mysql+pymysql://\
root:password@localhost/Geeks4Geeks")
  
# CREATE THE TABLE MODEL TO USE IT FOR QUERYING
class Students(Base):
  
    __tablename__ = 'students'
  
    first_name = db.Column(db.String(50), 
                           primary_key=True)
    last_name  = db.Column(db.String(50), 
                           primary_key=True)
    course     = db.Column(db.String(50))
    score      = db.Column(db.Float)
  
# CREATE THE SESSION OBJECT
Session = sessionmaker(bind=engine)
session = Session()
  
# SELECTING COLUMN `first_name`, `last_name` WHERE `score > 80`
result = session.query(Students) \
    .with_entities(Students.first_name, Students.last_name) \
        .filter(Students.score > 80).all()
  
for r in result:
    print(r.first_name, r.last_name)


Python3
import sqlalchemy as db
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
  
Base = declarative_base()
  
# DEFINE THE ENGINE (CONNECTION OBJECT)
engine = db.create_engine("mysql+pymysql://\
root:password@localhost/Geeks4Geeks")
  
# CREATE THE TABLE MODEL TO USE IT FOR QUERYING
class Students(Base):
  
    __tablename__ = 'students'
  
    first_name = db.Column(db.String(50),
                           primary_key=True)
    last_name  = db.Column(db.String(50), 
                           primary_key=True)
    course     = db.Column(db.String(50))
    score      = db.Column(db.Float)
  
# CREATE THE SESSION OBJECT
Session = sessionmaker(bind=engine)
session = Session()
  
# SELECTING COLUMN `first_name`, `score`
# WHERE `score > 80` AND `course` is STATISTICS
result = session.query(Students) \
    .with_entities(Students.first_name, Students.score) \
        .filter(Students.score > 80,
                Students.course.like('Statistics')).all()
  
for r in result:
    print(r.first_name, r.score)


输出:

输出 - 示例 1

示例 2:根据多个过滤器选择特定列

Python3

import sqlalchemy as db
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
  
Base = declarative_base()
  
# DEFINE THE ENGINE (CONNECTION OBJECT)
engine = db.create_engine("mysql+pymysql://\
root:password@localhost/Geeks4Geeks")
  
# CREATE THE TABLE MODEL TO USE IT FOR QUERYING
class Students(Base):
  
    __tablename__ = 'students'
  
    first_name = db.Column(db.String(50),
                           primary_key=True)
    last_name  = db.Column(db.String(50), 
                           primary_key=True)
    course     = db.Column(db.String(50))
    score      = db.Column(db.Float)
  
# CREATE THE SESSION OBJECT
Session = sessionmaker(bind=engine)
session = Session()
  
# SELECTING COLUMN `first_name`, `score`
# WHERE `score > 80` AND `course` is STATISTICS
result = session.query(Students) \
    .with_entities(Students.first_name, Students.score) \
        .filter(Students.score > 80,
                Students.course.like('Statistics')).all()
  
for r in result:
    print(r.first_name, r.score)

输出:

输出 - 示例 2