使用执行的 SQLAlchemy
我很难在手动查询中使用 group by、having 和 sum 。下面是我的查询,有人可以帮助我确定为什么它返回这么多行(就好像它没有应用分组依据或具有)。
q = meta.Session.execute('SELECT ID FROM table GROUP BY ID HAVING SUM(viewCount)= 0')
results = q.fetchall()
len(results)
上面的输出是 371
如果我运行常规 SQL 的查询
SELECT ID FROM table GROUP BY ID HAVING SUM(viewCount)=0;
输出是 找到 17 行
我尝试利用 SQLAlchemy 提供的 group_by 但无法让它工作正确的拥有和总和。我发现我可能需要包含 func sum 。非常感谢任何帮助。
I am having a hard time using group by, having, and sum all together in a manual query. Below is my query, can someone help me identify why it returns so many rows (as if it isn't applying the group by or having).
q = meta.Session.execute('SELECT ID FROM table GROUP BY ID HAVING SUM(viewCount)= 0')
results = q.fetchall()
len(results)
Output from the above is 371
If I run the query
SELECT ID FROM table GROUP BY ID HAVING SUM(viewCount)=0;
Output from regular SQL is 17 rows found
I tried utilizing the group_by that SQLAlchemy offers but I couldn't get it to work right with the having and the sum. I saw that I may need to include the func sum. Any help is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论