Let’s say I have a db of Customers. I want to fetch data and filter rows by exact pairs.
I want to do the following using sqlalchemy:
SELECT first_name, age
FROM Customers
WHERE (first_name, age) in (('John', '31'),('Robert', '22'));
I know I can do this:
SELECT first_name, age
FROM Customers
WHERE first_name in ('John', 'Robert') and age in ('31','22');
like this:
with get_mariadb_session() as session:
query = select(
Customers.name,
Customers.age,
Customers.email
).select_from(
Customers
).where(
and_(Customers.name.in_(names), Customers.age.in_(ages))
)
res = session.execute(query)
But this might return a result set with a record (‘John’,’22’) too.
Is it possible to do tuple matching in sqlalchemy?