首页 > 解决方案 > SQLite 子查询,具有可变数量的输入

问题描述

尝试为 SQLite 数据库构建 SQL 查询,该查询将获取主题名称并将返回所有匹配的行,而不管主题记录中的单词顺序如何。以下查询对 2 个输入按预期工作。

select name
from 
    (select name from students where tags like "%biology%") 
where 
    tags like "%physics%"

如果按如下方式传入 3 个主题输入,则Geo Math Zoo必须通过代码扩展上述查询(在这种情况下使用 Python)。有没有更好的选择来处理 9 到 10 范围内的输入案例数量?

Student桌子:

Name    Sub
-------------------------------------------
S1      Biology Math Geo Physics
S2      Math Geo Physics
S3      Biology Geo Math Physics Zoo       -- should be in output
S4      Biology Physics Math Geo 
S5      Biology Zoo Math Geo               -- should be in output

标签: sqlsqliteselect

解决方案


您可以使用OR运算符搜索多个条件:

select *
from students
where tags like "%biology%"
   or tags like "%physics%"

推荐阅读