Skip to content

Instantly share code, notes, and snippets.

@tell-k
Created September 14, 2012 15:47
Show Gist options
  • Save tell-k/3722779 to your computer and use it in GitHub Desktop.
Save tell-k/3722779 to your computer and use it in GitHub Desktop.
# AND OR とかも直感的に使える
from sqlalchemy.sql.expression import *
# AND
Entry.query.filter(and_(Entry.id == 1, Entry.text == "entry")).all()
# => SELECT * FROM entries WHERE entries.id = 1 AND entries.text = "entry"
# OR
Entry.query.filter(or_(Entry.id == 1, Entry.text == "entry")).all()
# => SELECT * FROM entries WHERE entries.id = 1 OR entries.text = "entry"
# IN
Entry.query.filter(Entry.id.in_([1, 2, 3])).all()
# => SELECT * FROM entries WHERE entries.id IN (1, 2, 3)
# NOT IN
Entry.query.filter(not_(Entry.id.in_([1, 2, 3]))).all()
# => SELECT * FROM entries WHERE entries.id NOT IN (1, 2, 3)
# LIKE
Entry.query.filter(Entry.text.like("ent%")).all()
# => SELECT * FROM entries WHERE entries.id LIKE "ent%"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment