简体   繁体   中英

How to use a list as parameter in clause WHERE in python and CosmoDB

i got a list of ids and i want to use this list as parameter like this:

list_id = ['4833', '43443', '431431']
qry = f"""SELECT
            c.nm_cnae as Nome_CNAE
            FROM cnae as c
            WHERE c.cod_cnae = '{list_id}'"""
resultado_busca = cosmosengine.query('cnae', qry)
resultado_busca = list(resultado_busca)

how should i do this works?

I'm using azure cosmosdb

Assuming you want to find all the ones that have an id in the list it would be:

list_id = ['4833', '43443', '431431']
qry = f"""SELECT
            c.nm_cnae as Nome_CNAE
            FROM cnae as c
            WHERE c.cod_cnae IN ({','.join(list_id)})"""
resultado_busca = cosmosengine.query('cnae', qry)
resultado_busca = list(resultado_busca)

This uses the IN operator in sql: https://www.w3schools.com/sql/sql_in.asp

','.join(list_id) creates a string where each value is separated by a comma.

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