简体   繁体   中英

How do I use query IN an array in python?

I'm still rather new to querying using Python.

So, I have a connection to a table in SMSS via Visual Studio Code. I took a column from the table, turned it into a dataframe, dropped the duplicates, and stored the result in an array with numPy. Now, my goal is to query and SELECT id FROM [my table] WHERE acc_num IN [my array].

I'm using pandas to query using the connection as well.

I'm a little lost on what the structure of the code should be when trying to access this array that it separate from the database itself. Any help is appreciated.

Try this, Assuming the array name is 'array'

tup_ar = tuple(array.flat)
tup_ar
query = "SELECT name FROM table_name WHERE field IN {}".format(tup_ar)
##Execute the query 

Execute the query string created using any of the functions available in python.

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