2016-10-25 43 views
0

我需要一些幫助將此查詢轉換爲SQLAlchemy。Python/SQLAlchemy - 需要將3個表的內部連接查詢轉換爲Python

select field from table t1 join table t2 on t1.detail_id = t2.id join table t3 on t3.id = t2.rate_id where t2.name = 'fred' and t3.rate_type = 'Custom' and t3.description = 'Default'; 

我已經能夠將內部連接查詢轉換爲兩個表,但需要一些幫助。

感謝您的幫助。

回答

-1

SQLAlchemy提供ORM方式和SQL方式來操作數據庫。您可以使用原始的SQL語言(或SQLAlachemy SQL Express)進行查詢。

(1)RAW SQL查詢,示例代碼:

engine = create_engine(...) 
q = 'SELECT foo FROM t_bar WHERE col_name=:v_parameters' 
rs = engine.execute(sqlalchemy.text(q), v_parameters=your_actual_value) 

檢查executebasic usage。也可以看看ResultProxy瞭解如何操作返回的結果。

(2)ORM。如果你想使用ORM,首先你必須定義模型和映射器類。示例代碼。

from sqlalchemy import Column, ForeignKey 
from sqlalchemy.types import String, Integer 
from sqlalchemy.orm import relationship 
from sqlalchemy.ext.declarative import declarative_base 


Base = declarative_base() 


class Father(Base): 
    id = Column(Integer, primary_key=True) 
    name = Column(String(31), unique=True, nullable=False) 
    # Relationship attributes 
    children = relationship('Sons', 
          passive_deletes=True, 
          back_populates='father') 


class Son(Base): 
    id = Column(Integer, primary_key=True) 
    name = Column(String(31), unique=True, nullable=False) 
    # foreign keys 
    p_id = Column(Integer, ForeignKey('Father.id', 
             ondelete='CASCADE', 
             onupdate='CASCADE')) 
    # Relationship attributes 
    parent = relationship('Father', 
          passive_deletes=True, 
          back_populates='sons') 

然後,你可以做ORM查詢操作

session.query(Father).join(Father.sons).filter(Son.name=='Sam') 

等於SQL查詢

SELECT father.id, father.name FROM father JOIN son ON father.id=son.p_id WHERE son.name='Sam'. 

請檢查ORM mapperORM Query以獲取更多信息。

適合您的應用程序。如果你已經很好地映射了所有的表格,那麼你可以使用ORM方式。如果你不需要ORM功能,你可以使用RAW SQL查詢。

謝謝。

0

如果簡單的SQL查詢的話,你可以嘗試:

session.execute("SELECT t1.field AS t1_field " 
    "FROM t1 JOIN t2 ON t1.detail_id = t2.id JOIN t3 ON t2.rate_id = t3.id " 
    "WHERE t2.name = :name AND t3.rate_type = :rate_type AND t3.description = :description", 
    {'name': 'fred', 'rate_type': 'Custom', 'description': 'Default'}) 

但是,如果你想使用SQLAlchemy聲明基地則查詢將如下所示:

results = session.query(T1.field).join(T2, T1.detail_id == T2.id).join(T3, T2.rate_id == T3.id).\ 
    filter(T2.name == 'fred').\ 
    filter(T3.rate_type == 'custom').\ 
    filter(T3.description == 'lorem ipsum').all() 

以下型號:

from sqlalchemy import create_engine, Integer, ForeignKey, String, Column 
from sqlalchemy.orm import sessionmaker 
from sqlalchemy.ext.declarative import declarative_base 

Base = declarative_base() 


class T1(Base): 
    __tablename__ = 't1' 
    id = Column(Integer, primary_key=True) 
    field = Column(String) 
    detail_id = Column(Integer, ForeignKey("t1.id")) 


class T2(Base): 
    __tablename__ = 't2' 
    id = Column(Integer, primary_key=True) 
    name = Column(String) 
    rate_id = Column(Integer, ForeignKey("t1.id")) 


class T3(Base): 
    __tablename__ = 't3' 
    id = Column(Integer, primary_key=True) 
    rate_type = Column(String) 
    description = Column(String) 

我希望它有幫助。

相關問題