首页 > 解决方案 > 在 SQLite3 中,如何选择匹配列表的任何项目?

问题描述

我使用“sqlite3”来访问 Python 程序中的数据库。我有一个关键字列表,例如,

fruit=['banana','apple','orange']   #the total number is 50. 

如果我有下表:

Col 1                       | Col 2
'this is a desk'            | something else
'banana is good for health' | something else
'bad apple'                 | something else

如何选择 Col 1 包含水果中的任何项目的行,例如在此示例中,应选择第 2 行和第 3 行?

我知道一个项目的“LIKE”命令,例如

SELECT * FROM table WHERE Col 1 LIKE '%banana%'

但我不知道如何使用列表来做到这一点。

标签: python-3.xsqlite

解决方案


使用OR运算符选择具有不同值的行。

SELECT "Col 1" 
FROM table 
WHERE 
"Col 1" LIKE '%banana%' OR 
"Col 1" LIKE '%apple%' OR 
"Col 1" LIKE '%orange%'

推荐阅读