如何在Sqlalchemy中使用不在两个字段中创建子查询?

时间:2015-09-28 19:59:52

标签: python sqlalchemy flask-sqlalchemy

我需要使用而不是进行过滤,但需要在两个字段中进行过滤。

q = db_session.query(Necessidade, WFLeilao, BidHeader, BidItemPrice, func.sbprecobruto(BidItemPrice.bid_number,BidItemPrice.line_number, Necessidade.identportal, type_=Float))
q = q.join(WFLeilao, and_(Necessidade.numeroportal == WFLeilao.leilao, Necessidade.numeroitemportal == WFLeilao.itemleilao))    
q = q.join(BidHeader, and_(BidHeader.bid_number == BidItemPrice.bid_number))    
q = q.join(BidItemPrice, and_(BidItemPrice.auction_header_id == WFLeilao.leilao, BidItemPrice.auction_line_number == WFLeilao.itemleilao, BidItemPrice.bid_number == WFLeilao.lance, BidItemPrice.line_number == WFLeilao.itemlance))    

subquery = db_session.query(ItfRetornoPedido.num_leilao_superbuy, ItfRetornoPedido.num_item_leilao_superbuy).filter_by(status_comprador=1).filter_by(acao='I').filter_by(empresa='NK').subquery()

q = q.filter(~(WFLeilao.leilao,Wfleilao.itemleilao).in_(subquery))

在oracle中是可能的,类似的例子:

 Select *
  from table_a
 where (leilao, itemleilao) not in
       (Select num_leilao_superbuy, num_item_leilao_superbuy
          from table_b 
         where empresa = 'NK')

有可能吗?

2 个答案:

答案 0 :(得分:2)

我找到了使用元组_

的解决方案
q = q.filter(~tuple_(WFLeilao.leilao, WFLeilao.itemleilao).in_(subquery))

答案 1 :(得分:1)

你可以链接查询:

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://www.speedyshare.com/")

driver.switch_to.frame(0)

upload_input = driver.find_element_by_css_selector("#selectfilebox input[type=file]")
upload_input.send_keys("/Users/user/Downloads/dr-evil-and-minion-laughing.png")

upload_button = driver.find_element_by_link_text("Upload")
upload_button.click()