데이터 필터링과 타기팅

Python으로 배우는 데이터베이스 입문

Jason Myers

Co-Author of Essential SQLAlchemy and Software Engineer

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
...
Python으로 배우는 데이터베이스 입문

WHERE 절

  • 불리언 조건으로 쿼리 반환 데이터를 제한합니다
  • 열을 값 또는 다른 열과 비교합니다
  • 자주 쓰는 비교: ==, <=, >=, !=
Python으로 배우는 데이터베이스 입문

표현식

  • 단순 연산자보다 복잡한 조건을 제공합니다
  • 예: in_(), like(), between()
  • 더 많은 내용은 문서 참고
  • Column의 메서드로 제공됨
Python으로 배우는 데이터베이스 입문

표현식

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
...
Python으로 배우는 데이터베이스 입문

연결자(Conjunctions)

  • WHERE 절에 여러 기준을 사용합니다
  • 예: and_(), or_(), not_()
Python으로 배우는 데이터베이스 입문

연결자(Conjunctions)

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
Python으로 배우는 데이터베이스 입문

Ayo berlatih!

Python으로 배우는 데이터베이스 입문

Preparing Video For Download...