繁体   English   中英

flask-sqlalchemy,将sql中的一条记录计数为一个关系,然后加入到一个users表中

[英]flask-sqlalchemy, sql count records in a relationship and then join to a users table

我用SQL以这种方式查询我的用户:

select * 
from users

我得到了所有用户,但现在我想查询他们有多少个朋友,所以如果我这样做

我这样做:

select count(*) 
from users inner join friendship on users.id = friendship.user

我确切地知道了他们有多少个朋友,但是现在我想将此记录添加到users表中,以便按此数字对其进行排序

有没有办法用sqlalchemy做到这一点?

select users.*, count(*) over (partition by friendship.user) 
from users inner join friendship on users.id = friendship.user
order by count

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM