4

I'm building a web2py controller in which I need to query a table for a combination of value x in one field AND value y in a second field (in the same row). To query on a single field, I would just write

db.table.field == x

But I don't know how to write a query that looks for field==x AND field2==y

monotasker
  • 1,985
  • 1
  • 17
  • 22
  • Have you tried `(db.table.field == x)&(db.table.field2 == y)` ? It's been a while since I've done web2py stuff but I thought that was how it worked... – mal-wan Aug 30 '11 at 01:41

2 Answers2

5
(db.table.field1==x)&(db.table.field2==y)

See the book section on logical operators.

Anthony
  • 25,466
  • 3
  • 28
  • 57
1

For a more advanced version, you can append a query to a list and use python's reduce function.

queries=[]
if arg1 == "x": queries.append(db.table.field == x)
if arg2 == "y": queries.append(db.table.otherfield == y)
# many conditions here....
query = reduce(lambda a,b:(a&b),queries)
db(query).select()