Resultaten sorteren

Introductie tot databases in Python

Jason Myers

Co-Author of Essential SQLAlchemy and Software Engineer

Order by-clausules

  • Bepaalt de volgorde waarin records terugkomen
  • Als statement-methode: order_by()
Introductie tot databases in Python

Oplopend sorteren

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',), ...]
Introductie tot databases in Python

Aflopend sorteren

  • Wikkel de kolom in desc() binnen order_by()
Introductie tot databases in Python

Sorteren op meerdere

  • Scheid meerdere kolommen met een komma
  • Sorteert volledig op de eerste kolom
  • Bij dubbelen in de eerste kolom: sorteert op de tweede
  • Herhaal tot alle kolommen gesorteerd zijn
Introductie tot databases in Python

Sorteren op meerdere

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')
Introductie tot databases in Python

Laten we oefenen!

Introductie tot databases in Python

Preparing Video For Download...