SQLite子查询,输入数量可变

时间:2019-11-28 01:03:23

标签: sql sqlite select

尝试为SQLite数据库构建SQL查询,该查询将使用给出的主题名称,并将返回所有匹配的行,而与主题记录中的单词顺序无关。以下查询可正常使用2个输入。

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

如果按照以下Geo Math Zoo的方式传递了3个主题输入,则必须通过代码(在这种情况下使用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

1 个答案:

答案 0 :(得分:1)

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

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