chrizonline
chrizonline

Reputation: 4979

How can a SQLAlchemy query use MySQL's REGEXP operator?

How can this SQL query:

SELECT * from table where field REGEXP 'apple|banna|pear';

be written using SQLAlchemy?

My base query looks like this:

query = session.query(TableFruitsDTO)

Upvotes: 4

Views: 3985

Answers (1)

davidism
davidism

Reputation: 127360

The SQLAlchemy docs describe how to use the MySQL REGEXP operator. Since there is no built-in function, use .op() to create the function:

query = session.query(TableFruitsDTO).filter(
     TableFruitsDTO.field.op('regexp')(r'apple|banana|pear')
)

Upvotes: 10

Related Questions