Filtrování a cílení dat

Introduction to Databases in Python

Jason Myers

Co-Author of Essential SQLAlchemy and Software Engineer

Klauzule WHERE

stmt = select([census])

stmt = stmt.where(census.columns.state == 'California')
results = connection.execute(stmt).fetchall()
for result in results: print(result.state, result.age)
California 0
California 1
California 2
California 3
California 4
California 5
...
Introduction to Databases in Python

Klauzule WHERE

  • Omezuje vrácená data dotazu pomocí booleovských podmínek
  • Porovnává sloupec s hodnotou nebo jiným sloupcem
  • Často využívá operátory ==, <=, >= nebo !=
Introduction to Databases in Python

Výrazy

  • Umožňují složitější podmínky než jednoduché operátory
  • Např. in_(), like(), between()
  • Více v dokumentaci
  • Dostupné jako metoda na objektu Column
Introduction to Databases in Python

Výrazy

stmt = select([census])

stmt = stmt.where(census.columns.state.startswith('New'))
for result in connection.execute(stmt): print(result.state, result.pop2000)
New Jersey 56983
New Jersey 56686
New Jersey 57011
...
Introduction to Databases in Python

Konjunkce

  • Umožňují více kritérií v klauzuli WHERE
  • Např. and_(), or_(), not_()
Introduction to Databases in Python

Konjunkce

from sqlalchemy import or_

stmt = select([census])
stmt = stmt.where( or_(census.columns.state == 'California', census.columns.state == 'New York' ) )
for result in connection.execute(stmt): print(result.state, result.sex)
New York M
...
California F
Introduction to Databases in Python

Pojďme si procvičit!

Introduction to Databases in Python

Preparing Video For Download...