On Sep 20, 2008, at 7:59 AM, m.talaee wrote:

>
> hi all
> i need to sort users by number of their friends.
> there is many to many relation table between user table and itself.
> order_by don't support ordering (or i couldn't find).
> please help me

for a mapping like:

mapper(User, users, properties={
        'friends':relation(User, secondary=user_friends, primaryjoin=....,  
secondaryjoin=...)
})


you can say:

subq =  
select 
([func 
.count 
(user_friends 
.c.friend_id).label('friendcount')]).group_by(user_friends.c.user_id)

sess.query(User).select_from(users.join(subq,  
users.c.user_id==subq.c.user_id)).order_by(subq.c.friendcount)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to