擅长:python、mysql、java
<p>如果您的查询只是为了让拥有80%以上子女的父母顽皮,您可以在大多数数据库中将<code>naughty</code>转换为整数,然后取其平均值;然后<code>having</code>这个平均值大于<code>0.8</code>。在</p>
<p>所以你得到了</p>
<pre><code>from sqlalchemy.sql.expression import cast
naughtyp = func.avg(cast(Child.naughty, Integer))
session.query(Parent, func.count(Child.id), naughtyp).join(Child)\
.group_by(Parent.id).having(naughtyp > 0.8).all()
</code></pre>