[英]How to do advance joins when using sqlalchemy?
Could someone show me how I can accomplish this in sqlalchemy? 有人可以告诉我如何在sqlalchemy中做到这一点吗?
SELECT
*
FROM animals a
INNER JOIN species s
ON (s.species_id=a.id AND s.type='mammals')
LIMIT 1;
I have tried many different things, but I keep getting weird results. 我尝试了许多不同的方法,但结果却一直很奇怪。 If someone please help me with this I would greatly appreciate this.
如果有人请帮助我,我将不胜感激。
Some code I have done: 我完成的一些代码:
result = session.query(Animal).\
join((Species, (Species.species_id==Animal.id)), (Species, (Species.type=='mammals')))
.all()
This of course gives me: 这当然给了我:
SELECT
*
FROM animals
INNER JOIN species
ON (species.species_id=animals.id)
INNER JOIN species
ON (AND species.type='mammals')
LIMIT 1;
But this is not exactly what I want. 但这不是我想要的。
Thanks in advance. 提前致谢。
-e -e
You just need and_
: 您只需要
and_
:
from sqlalchemy.sql import and_
result = session.query(Animal).join(Species, and_(
Species.species_id == Animal.id,
Species.type == 'mammals',
)).all()
EDIT: this is using the "two-argument calling form" of join
available in SQLAlchemy 0.7 and later. 编辑:这是利用“两个参数调用形式”的
join
在SQLAlchemy的0.7及更高版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.