5

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...

3 Answers 3

7

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)
)
Sign up to request clarification or add additional context in comments.

1 Comment

@A H M Forhadul Islam This is not useful as it deters other users from posting the right answer. I clearly stated in my question (in bold) that I am not interested in the orm. You might leave your answer though, as it might be helpful to others.
1

Try reading this part of the documentation -
http://docs.sqlalchemy.org/en/latest/core/sqlelement.html#sqlalchemy.sql.expression.ColumnElement.in_

Comments

0
from sqlalchemy import tuple_
stmt = select([mytable]).where(tuple_(mytable.c.symbol,mytable.c.volume)
                               .in_([('ES',238),('GB',500),('OH',800)]))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.