简体   繁体   中英

How to use MySQL SOUNDEX function with SQLAlchemy

Looking for any example of making SOUNDEX queries on MySQL from SQLAlchemy, if possible at all. Any alternatives?

If all you need is to use the SOUNDEX() function, then just use func to generate the function expression:

session.query(func.soundex(MyModel.some_str))

If on the other hand you need the SOUNDS LIKE operator, you can use op() :

session.query(MyModel).\
    filter(MyModel.some_str.op('SOUNDS LIKE')('Supercalifragilisticexpialidocious'))

which is equivalent to

session.query(MyModel).\
    filter(func.soundex(MyModel.some_str) ==
           func.soundex('Supercalifragilisticexpialidocious'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM