[英]How to intersect two raw SQL queries using SQLAlchemy?
我有两个原始的Postgresql语句。
q1 = "SELECT name FROM table_a"
q2 = "SELECT name FROM table_b"
如何使用SQLAlchemy查询它们的交集。
这或多或少是您可以做的:
from sqlalchemy import create_engine, text
engine = create_engine('postgresql://user:password@localhost/somedb')
q1 = "SELECT name FROM table_a intersect SELECT name FROM table_b"
q1res = engine.execute(text(q1)).fetchall()
# or
q2res = engine.execute(text(q1)).fetchone()
根据您的需要。 如果要插入或更新,则fetchall
使用fetchone
和fetchall
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.