在sqlalchemy中使用带有参数的子查询

时间:2013-10-22 00:52:44

标签: python mysql sql

我正在尝试运行一个使用子查询来表示结果集列的查询。

select id_column1, id_column2, id_column3,
(select column4 from table2 where id in (id_column1, id_column2, id_column3)
order by id desc
limit 1) as column4
from table1
join table2 on table1.id = table2.id
join table3 on table2.id = table2.id
where clause...    

我所拥有的sqlalchemy等同于

order_state = aliased(Table1)
po_state = aliased(Table1)
proof_state = aliased(Table1)

subq = meta.Session.query(Table2.exception_message).\
  filter(Table2.entity_id.in_([Table3.id, Table4.id, Table5.id])).\
  order_by(Table2.id.desc()).\
  limit(1).label('exception')

query = meta.Session.query(
  Table3.po_number, Table4.id.label('po_id'), Order.id.label('order_id'),\
  Table5.id.label('proof_id'), order_state.description.label('order_state'),\
  po_state.description.label('po_state'), proof_state.description.label('proof_state'),
  Table3.order_placement_date, subq).\
  outerjoin(Table4, Table3.id == Table4.order_id).\
  outerjoin(Table5, Table4.id == Table5.po_id).\
  outerjoin(order_state, Table3.state_diagram_state_id == order_state.id).\
  outerjoin(po_state, Table4.state_diagram_state_id == po_state.id).\
  outerjoin(proof_state, Table5.state_diagram_state_id == proof_state.id).\
  filter(Table3.is_placed != 0).\
  filter(not_(Table3.po_number.contains('%-%'))).\
  filter(not_(Table3.state_diagram_state_id.in_(Table1.NONE_ORDER_EXCEPTIONS))).\
  filter(or_(Table3.state_diagram_state_id.in_(Table1.ORDER_EXCEPTIONS),\
    Table4.state_diagram_state_id.in_(Table1.PO_EXCEPTIONS),\
    Table5.state_diagram_state_id.in_(Table1.PROOF_EXCEPTIONS)))

如何在sql alchemy中使用子查询,该子查询使用您要查询的列作为该过滤器的参数?

非常感谢。

1 个答案:

答案 0 :(得分:0)

(根据引用的sqlfiddle编辑)

from sqlalchemy import create_engine

sql = """
CREATE TABLE Table1
     (
     id int primary key,
     type varchar(20),
     details varchar(30)
    );

CREATE TABLE Table2
     (
     id int primary key,
     type varchar(20),
     details varchar(30)
    );

CREATE TABLE Table3
     (
     id int primary key,
     type varchar(20),
     details varchar(30)
    );

CREATE TABLE Table4
     (
     id int primary key,
     entity_id int,
     type varchar(20),
     details varchar(30)
    );

INSERT INTO Table1 (type, details) values ('data1', 'details of data1');

INSERT INTO Table2 (type, details) values ('data2', 'details of data2');

INSERT INTO Table3 (type, details) values ('data3', 'details of data3');

UPDATE Table2 SET id = 2 where id = 1;
UPDATE Table3 SET id = 3 where id = 1;

INSERT INTO Table4 (entity_id, type, details) values (1, 'exception', 'err 1');
INSERT INTO Table4 (entity_id, type, details) values (2, 'exception2', 'err 2');
INSERT INTO Table4 (entity_id, type, details) values (3, 'exception3', 'err 3');

"""

e = create_engine("sqlite://", echo=True)

for stmt in sql.split(";"):
    e.execute(stmt.strip())

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.declarative import DeferredReflection
Base = declarative_base(cls=DeferredReflection)

class Table1(Base):
    __tablename__ = 'Table1'

class Table2(Base):
    __tablename__ = 'Table2'

class Table3(Base):
    __tablename__ = 'Table3'

class Table4(Base):
    __tablename__ = 'Table4'

Base.prepare(e)

from sqlalchemy.orm import Session
s = Session(e)

subq = s.query(Table4.type).filter(Table4.entity_id.in_([Table1.id, Table2.id, Table3.id]))
q = s.query(
        Table1.type,
        Table2.type,
        Table3.type,
        subq.label('table4type')
    ).join(Table2, Table1.id == Table2.id).join(Table3, Table2.id == Table3.id)
print q.all()