Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, sqlalchemy, Select with WHERE IN clause

Using Python and sqlalchemy:

How can I achieve the following MySQL statement with a WHERE ... IN clause using a tuple?

SELECT * FROM mytable WHERE (symbol, volume) IN (('ES', 238 ),('GB', 500 ),('OH', 800 ));

in sqlalchemy core (ie not the ORM version)

I looked in the documentation and generally on SO/google, this is nowhere to be found...

like image 844
jim jarnac Avatar asked Mar 10 '23 08:03

jim jarnac


1 Answers

Assuming you use the declarative style (i.e. ORM classes). For your table there should be a orm class. I am assuming it as MyTable. Then the code will be like this:

keys = [
    ('ES', 238 ),('GB', 500 ),('OH', 800 )
]

select([
    MyTable.url
]).select_from(
    MyTable
).where(
    tuple_(MyTable.symbol, MyTable.volume).in_(keys)
)
like image 75
Forhadul Islam Avatar answered Mar 24 '23 10:03

Forhadul Islam