简体   繁体   English

如何在Python中加入2个查询

[英]How to join 2 queries in Python

I am in Python and I have the above 2 queries which I want to join them in 1. 我在Python中,我有以上两个查询,我想将它们加入1。

def fly_away(session, line_id):
    query = session.\
              query(
                Pilot.Id,
                Pilot.EntityId,
                Pilot.From
              ).\
    filter(Pilot.Id == line_id)

    query = session.\
                  query(
                    CTower.Id,
                    CTower.Time,
                    CTower.Associate
                  ).\
    filter(CTower.Associate == line_id)

I tried to use join but I dont know how to use it correctly. 我尝试使用连接,但我不知道如何正确使用它。 Can I have some help please? 我可以帮忙吗?

I don't know what database are you using and what driver are you using to connect python to the database but I will post the sql query that will do the job. 我不知道您使用的是哪个数据库,以及使用什么驱动程序将python连接到数据库,但是我将发布执行该工作的sql查询。 I am sure, you can use it with your driver. 我敢肯定,您可以将其与驱动程序一起使用。

SELECT Pilot.Id, Pilot.EntityId, Pilot.From, CTower.Id, CTower.Time, CTower.Associate 
FROM Pilot, CTower 
WHERE CTower.Associate == Pilot.Id
def fly_away(session, line_id):
  query = session.\
          query(
            Pilot.Id,
            Pilot.EntityId,
            Pilot.From,
          ).\
          outerjoin(CTower, Pilot.Id == CTower.Associate).\
          filter(Pilot.Id == line_id)     
  return query

this is what I am trying now.. and its working .. but not exactly as I want 这是我现在正在尝试的..及其工作..但不完全是我想要的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM