简体   繁体   English

如何在python和CosmoDB中的WHERE子句中使用列表作为参数

[英]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:我得到了一个 id 列表,我想使用这个列表作为参数,如下所示:

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我正在使用 azure cosmosdb

Assuming you want to find all the ones that have an id in the list it would be:假设您想在列表中找到所有具有 id 的对象,它将是:

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这在 sql 中使用 IN 运算符: https : //www.w3schools.com/sql/sql_in.asp

','.join(list_id) creates a string where each value is separated by a comma. ','.join(list_id)创建一个字符串,其中每个值都用逗号分隔。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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