简体   繁体   中英

Python sqlite3 INNER JOIN with a python list

I am trying to select from a table where values of one of the columns is equal to values of a list. For example:

TABLE
------------
ID    price
a     100
b     200
...
z     2600

python list: ["a", "d" , "e"]. And i want to find the prices of each of those IDs. The obvious way of doing this is to do a JOIN on ID but that list is not a table. what should i do?

You could write the list to a temporary table, and join with that. To make the join more efficient, ensure that at least one of the join columns (preferrably that of the smaller table) is indexed.

However, if the list is not too long, you can simply use the IN operator :

SELECT *
FROM MyTable
WHERE ID IN ('a', 'd', 'e')

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