मुझे अपने स्क्लेक्लेमी प्रश्नों को अनुकूलित करने में कठिनाई हो रही है। मेरा एसक्यूएल ज्ञान बहुत बुनियादी है, और मुझे बस एसक्यूएलकेमी डॉक्स से सामान की आवश्यकता नहीं मिल सकती है।SQLAlchemy: एक प्रश्न में कई मायने रखता है
class Parent(Base):
__tablename__ = "parents"
id = Column(Integer, primary_key = True)
children = relationship("Child", backref = "parent")
class Child(Base):
__tablename__ = "children"
id = Column(Integer, primary_key = True)
parent_id = Column(Integer, ForeignKey("parents.id"))
naughty = Column(Boolean)
मैं कैसे कर सकता है:
निम्नलिखित बहुत ही बुनियादी एक-से-अनेक संबंध मान लीजिए
- हर माता-पिता के लिए
(Parent, count_of_naughty_children, count_of_all_children)
की क्वेरी tuples?
सभ्य समय के बाद googling खर्च, मैंने पाया अलग से उन मूल्यों को क्वेरी करने के लिए कैसे:
# The following returns tuples of (Parent, count_of_all_children):
session.query(Parent, func.count(Child.id)).outerjoin(Child, Parent.children).\
group_by(Parent.id)
# The following returns tuples of (Parent, count_of_naughty_children):
al = aliased(Children, session.query(Children).filter_by(naughty = True).\
subquery())
session.query(Parent, func.count(al.id)).outerjoin(al, Parent.children).\
group_by(Parent.id)
मैं उन्हें अलग अलग तरीकों से गठबंधन करने की कोशिश की, लेकिन मैं क्या चाहते हैं पाने के लिए प्रबंधन नहीं किया।
- सभी माता-पिता से पूछें जिनमें 80% से अधिक शरारती बच्चे हैं? संपादित करें: शरारती पूर्ण हो सकता है।
मुझे लगता है कि यह क्वेरी पिछले एक पर आधारित होगी, शरारती/सभी अनुपात द्वारा फ़िल्टरिंग।
किसी भी मदद की सराहना की जाती है।
संपादित करें: अंटी Haapala की मदद के लिए धन्यवाद, मैंने पाया दूसरा सवाल का हल:
avg = func.avg(func.coalesce(Child.naughty, 0)) # coalesce() treats NULLs as 0
# avg = func.avg(Child.naughty) - if you want to ignore NULLs
session.query(Parent).join(Child, Parent.children).group_by(Parent).\
having(avg > 0.8)
यह औसत पाता है बच्चों के naughty
चर, झूठी इलाज और 0 के रूप में NULLs, और यह सच है के रूप में 1. परीक्षण किया MySQL बैकएंड के साथ, लेकिन दूसरों पर भी काम करना चाहिए।
को ठीक करने में मदद की, धन्यवाद, यह पूरी तरह से काम करता है! – meandrobo