简体   繁体   English

Python sqlite3 INNER JOIN与python列表

[英]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"]. python列表:[“ a”,“ d”,“ e”]。 And i want to find the prices of each of those IDs. 我想找到每个ID的价格。 The obvious way of doing this is to do a JOIN on ID but that list is not a table. 这样做的明显方法是对ID执行JOIN,但该列表不是表。 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 : 但是,如果列表不太长,则可以使用IN运算符

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

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

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