我有两个原始Postgresql语句。
q1 = "SELECT name FROM table_a"
q2 = "SELECT name FROM table_b"如何使用SQLAlchemy查询它们的交集。
发布于 2016-02-09 22:42:54
这或多或少是你能做的:
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()取决于你的需要。如果您打算进行插入或更新,请丢弃fetchone和fetchall。
https://stackoverflow.com/questions/35303406
复制相似问题