क्वेरी परिणामों का ऑर्डर करना

Python में Databases का परिचय

Jason Myers

Co-Author of Essential SQLAlchemy and Software Engineer

Order by क्लॉज़

  • क्वेरी परिणामों {{1}} में रिकॉर्ड किस क्रम में लौटें, इसे नियंत्रित करता है
  • स्टेटमेंट्स पर एक मेथड के रूप में उपलब्ध: order_by()
Python में Databases का परिचय

आरोही क्रम में Order by

print(results[:10])
[('Illinois',), ...]
stmt = select([census.columns.state])

stmt = stmt.order_by(census.columns.state)
results = connection.execute(stmt).fetchall()
print(results[:10])
[('Alabama',), ...]
Python में Databases का परिचय

अवरोही क्रम में Order by

  • order_by() क्लॉज़ में कॉलम को desc() से रैप करें
Python में Databases का परिचय

कई कॉलमों पर Order by

  • कई कॉलमों को कॉमा से अलग करें
  • पहले कॉलम से पूरा ऑर्डर होता है
  • फिर पहले कॉलम में डुप्लिकेट हों तो दूसरे कॉलम से ऑर्डर होता है
  • ऐसे तब तक दोहराएँ जब तक सभी कॉलम ऑर्डर न हो जाएँ
Python में Databases का परिचय

कई कॉलमों पर Order by

print(results)
('Alabama', 'M')
stmt = select([census.columns.state, census.columns.sex])

stmt = stmt.order_by(census.columns.state, census.columns.sex)
results = connection.execute(stmt).first() print(results)
('Alabama', 'F')
('Alabama', 'F')
...
('Alabama', 'M')
Python में Databases का परिचय

अभ्यास करते हैं!

Python में Databases का परिचय

Preparing Video For Download...